IOS学习之UiTableView下拉刷新与自动加载更多,百年不变的效果(

五一劳动节马上来临,小伙伴有妹有很激动哟,首先祝天下所有的程序猿节日快乐!这个五一对于我来说有点不一样,我的人生从这个五一就转弯了,爱情长跑8年的我结婚了,一会支付宝账号我会公布出去,请自觉打款!谢谢合作。

灯光闪起来:

舞蹈跳起来:

歌曲唱起来:

———————————————————————————————————————————————————-

智能世界的里面,做多的是什么?对,是APP,铺天盖地的APP随之袭来,APP死亡潮也随之出现,我们生活在互联网的时代里,每个人都是产品经理,据说,我们每天用的APP不超过8个,反正我每天必用百度云,你懂得,虽然APP多,但是他们都有一个共同点,你们知道是什么?你们千万不要告诉我是用手机玩的,他们就是都下拉刷新和加载更多,这些产品经理所有的共同点,虽然下拉刷新的动画都不一样,但是他们的目的只有个,,就是获取服务器最最新的数据,我们都知道Android里面有很多开源的下拉刷新的第三方比如有名的:Android-PullToRefresh。这个开源的很强大啊,支持很多控件,ListView、ViewPager、WevView、ExpandableListView、GridView、(Horizontal )ScrollView、Fragment

我们发现我们不需要上拉就可以自动加载更多,我感觉这种用户体验比较好:

继承步骤:

1:首先我们需要把MJRefresh这个开源的项目下载,把里面

全部考进自己的项目中,要先把这个文件拷贝到自己的项目的目录中,在xcode上右键add file这个文件我们才能把这个所需要的文件考进来,

2:实现:直接看代码:

//// ViewController.m// MyUitableViewRefreshAndMore//// Created by xiaoyuan on 15/4/28.// Copyright (c) 2015年 xiaoyuan. All rights reserved.//#import "MyUitableViewRefreshViewController.h"#import "MJRefresh.h"#import "DataSingleton.h"static const CGFloat MJDuration = 1.0;//#define MJRandomData [NSString stringWithFormat:@"随机数据—%d", arc4random_uniform(1000000)]#define DISPATCH_TIME_NOW (0ull)#define NSEC_PER_SEC 1000000000ull@interface MyUitableViewRefreshViewController ()<UITableViewDelegate,UITableViewDataSource>{UITableView *table;NSMutableArray *data;//判断是否加载完成BOOL isLoadIOver;}@end@implementation MyUitableViewRefreshViewController- (void)viewDidLoad {[super viewDidLoad];self .view .backgroundColor = [UIColor whiteColor];self.navigationController.navigationBar.barTintColor = [UIColor redColor];UILabel*title = [[UILabel alloc] initWithFrame:CGRectZero];title.text = @"刷新吧小宇宙";title.backgroundColor =[UIColor clearColor];title.textColor = [UIColor whiteColor];self.navigationItem.titleView = title;[title sizeToFit];[self.navigationController.navigationBar setTranslucent:NO];__weak typeof(self) weakSelf = self;if(data == nil){data = [[NSMutableArray alloc] init];}self.view.backgroundColor = [UIColor whiteColor];table = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds];table.backgroundColor =[UIColor whiteColor];table.separatorStyle = UITableViewCellEditingStyleNone;table.delegate =self;table.dataSource =self;[self.view addSubview:table];[table addLegendHeaderWithRefreshingBlock:^{[weakSelf loadNewData :YES];}];// 进入自动刷新[table.legendHeader beginRefreshing];}- (void)loadNewData:(BOOL)reresh{isLoadIOver = NO;//刷新移除所有数据if(reresh){[data removeAllObjects];}// 1.添加假数据for (int i = 0; i<20; i++) {[data addObject:@(i)];}// 2.模拟2秒后刷新表格UI(真实开发中,可以移除这段gcd代码)dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(MJDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{// 刷新表格[table reloadData];isLoadIOver = YES;// 拿到当前的下拉刷新控件,结束刷新状态[table.header endRefreshing];});}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{//只要大于的数是小于第一次返回的个数就行return data.count > 10? data.count + 1 : data.count;}//-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{return 1;}-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{return 44;}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{if([data count]>0){if(indexPath.row<[data count]){static NSString *CellIdentifier =@"Cell";UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];if(cell == nil){cell = [[ UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];//避免点击把分割线搞掉cell.selectionStyle = UITableViewCellSelectionStyleNone;//这个千万要记住写小于44,因为系统默认的就是44 ,这个把我坑坏了,一直找不到原因,一直以为重用的原因//大于44就超出高度,系统就给回收掉了UIView*view= [[UIView alloc] initWithFrame:CGRectMake(0, 43, 400, 1)];view.backgroundColor = [UIColor grayColor];[cell addSubview:view];}cell.textLabel.text = [NSString stringWithFormat:@"%@",[data objectAtIndex:indexPath.row]];cell .textLabel.textAlignment = NSTextAlignmentCenter;return cell;}}//加载更多if(isLoadIOver){[self loadNewData:NO];}return [DataSingleton.Instance getLoadMoreCell:table andIsLoadOver:NO andLoadOverString:@"正在加载…" andLoadingString:(YES ? @"正在加载" : @"下面10个") andIsLoading:YES];}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.}@end一切伟大的行动和思想,都有一个微不足道的开始

IOS学习之UiTableView下拉刷新与自动加载更多,百年不变的效果(

相关文章:

你感兴趣的文章:

标签云: