IOS开发之下拉刷新和上拉加载更多
IOS开发之下拉刷新和上拉加载更多
1.简介
常用的下拉刷新和上拉加载更多的实现方式
(1)UIRefreshControl
(2)EGOTableViewRefresh
(3)AH3DPullRefresh
(4)MJRefresh
(5)自己实现
2.AH3DPullRefresh的使用
2.1 配置
1.导入AH3DPullRefresh工程文件
2.在工程TARGETS-> Build Phases-> Compile Sources->
搜索UIScrollView+AH3DPullRefresh并在后面加上 “-fno-objc-arc”
3.添加头UIScrollView+AH3DPullRefresh的头文件
#import "UIScrollView+AH3DPullRefresh.h"
2.2 使用(基于满座网的实例)
1.添加下拉刷新和上拉加载更多的方法
#pragma mark 下拉刷新和上拉加载更多 -(void)addPullRefreshAndPullLoadMore{ [_tableView setPullToRefreshHandler:^{ //从第一行数据开始下载 _offset = 1; [self startDownloadData]; }]; [_tableView setPullToLoadMoreHandler:^{ _offset += _pageSize; [self startDownloadData]; }]; }
2. 下载数据:如果是下拉刷新先清空所有的数据
//如果_offset = 1,重新刷新 if (_offset == 1) { [_dataArray removeAllObjects]; }
加载完成新的数据之后结束刷新并刷新表格
//结束刷新 [_tableView refreshFinished]; [_tableView loadMoreFinished]; [_tableView reloadData];
3.完整的代码:
-(void)startDownloadData { NSString *urlStr = [NSString stringWithFormat:MAIN_SHOP_URL,_city,_categotry,_offset,_pageSize]; _reauest = [[HttpRequest alloc]init]; [_reauest requestWithUrl:urlStr targe:self action:@selector(dealDownloadFinish:)]; } -(void)dealDownloadFinish:(HttpRequest *)request{ NSString *str = [[NSString alloc]initWithData:request.data encoding:NSUTF8StringEncoding]; NSLog(@"%@",str); GDataXMLDocument *doc = [[GDataXMLDocument alloc]initWithData:request.data options:0 error:nil]; NSArray *list = [doc nodesForXPath:@"//promotion" error:nil]; //如果_offset = 1,重新刷新 if (_offset == 1) { [_dataArray removeAllObjects]; } for (GDataXMLElement *promotion in list) { PromotionModel *model = [[PromotionModel alloc]init]; model.myid = [[[promotion elementsForName:@"id"] firstObject] stringValue]; model.name = [[[promotion elementsForName:@"name"] firstObject] stringValue]; [_dataArray addObject:model]; } //结束刷新 [_tableView refreshFinished]; [_tableView loadMoreFinished]; [_tableView reloadData]; }