作为一个iOS的开发人员,总是感觉刷新这个东西不是很难,但是每次都要找控件比较闹心,所以在这记录一下上拉下拉的代码,以后就可以看自己的了吼吼……废话不多说上代码:
MJRefresh建议pod,做开发的都知道这里就不多讲了
导入MJRefresh//
// FristBaseViewController.m
// ProgramFrame
//
// Created by Mac on 2020/1/13.
// Copyright © 2020 当家洋少. All rights reserved.
//
#import "FristBaseViewController.h"
#import "FristBaseTableViewCell.h"
@interface FristBaseViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, strong)NSMutableArray * dataArray;
@end
@implementation FristBaseViewController
-(void)viewWillAppear:(BOOL)animated{
[superviewWillAppear:animated];
//进入页面就刷新
if(self.update) {
[self.tableView.mj_header beginRefreshing];
self.update=NO;
}
}
- (void)viewDidLoad {
[super viewDidLoad];
self.dataArray = [NSMutableArray array];
self.update=YES;
self.currentPage = 0;
//下拉刷新
__weak FristBaseViewController * weakSelf = self;
self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//刷新数据
[weakSelfloadlessData];
}];
//上拉加载
self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
[weakSelfloadMoreData];
}];
}
// ------下拉刷新
-(void)loadlessData{
self.currentPage = 0;
[self loadDataForType:1];
}
//上拉加载
-(void)loadMoreData{
intpageCount =self.totalProperty%LimitCount==0? (int)self.totalProperty/LimitCount: (int)self.totalProperty/LimitCount+1;
if(self.currentPage>= pageCount) {
[self.tableView.mj_footer endRefreshingWithNoMoreData];
return;
}
[self loadDataForType:2];
}
//返回的时候直接就写- (NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section {
return self.dataArray.count;
}
//解析int type判断是否是第一次
-(void)loadDataForType:(int)type{
NSMutableDictionary * params = [NSMutableDictionary dictionary];
params[@"channelId"] =@"";
params[@"limit"] =@(LimitCount);
params[@"page"] =@(_currentPage);
[[CDHttpHelper defaultCDHttpHelper] get:@"自己的接口" params:params success:^(id dic) {
__strongtypeof(self) strongSelf =self;
//totalProperty是总条数
strongSelf.totalProperty= [[dic objectForKey:@"totalProperty"]intValue];
//在这里处理数据
[strongSelfhandleData:dicwithType:type];
if(type ==1) {
self.currentPage=1;
if(strongSelf.totalProperty/LimitCount==0) {
[self.tableView.mj_footer endRefreshingWithNoMoreData];
}
}else{
self.currentPage+=1;
[self.tableView.mj_footer endRefreshing];
}
[self.tableView.mj_header endRefreshing];
[self.tableViewreloadData];
}failure:^(NSError*error) {
NSLog(@"%@",error);
}];
}
#pragma mark- data handle
-(void)handleData:(id) dicwithType:(int)type{
NSMutableArray * tempNews = [NSMutableArray array];
if(type ==1) {
self.dataArray= tempNews;
for(NSDictionary *dict in dic[@"data"][@"news"]) {
FristModel*model = [[FristModel alloc]init];
[model setValuesForKeysWithDictionary:dict];
[self.dataArray addObject:model];
}
}else{
for(NSDictionary*dictindic[@"data"][@"news"]) {
FristModel*model = [[FristModel alloc]init];
[model setValuesForKeysWithDictionary:dict];
[tempNewsaddObject:model];
}
[self.dataArrayaddObjectsFromArray:tempNews];
}
//处理暂无数据
[FristBaseViewController createNoMessageWithArray:self.dataArray tableView:self.tableView];
}
+ (void)createNoMessageWithArray:(NSMutableArray*)dataArraytableView:(UITableView*)tableView{
if(dataArray.count==0) {
UILabel *careLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 40)];
careLabel.text=@"暂无数据";
careLabel.textColor = [UIColor lightGrayColor];
careLabel.font= [UIFontsystemFontOfSize:16];
careLabel.textAlignment = NSTextAlignmentCenter;
tableView.tableFooterView= careLabel;
}else{
tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}
}
- (void)tableView:(UITableView*)tableViewwillDisplayCell:(UITableViewCell*)cellforRowAtIndexPath:(NSIndexPath*)indexPath
{
//予加载
if(self.dataArray.count>=30&&self.dataArray.count- indexPath.row<10&& !self.update)
{
self.update=YES;
[selfloadMoreData];
}
}
大致就这样了,有什么不懂的尽管问11903354@qq.com
希望各位大神不喜勿喷
第一次写这个东西,希望大家支持
网友评论