項目需求
我的的列表需要改變,原來的分頁加載采用的是MJRefresh框架進行加載更多數(shù)據(jù),這需要有一個上拉動作才能觸發(fā),而我的產(chǎn)品的意思是當快要滑動到底部時自動加載下一頁數(shù)據(jù)。我自己看了一下,發(fā)現(xiàn)很多app都是采用這種模式。
關(guān)于MJRefresh
MJRefresh中并沒有這樣的方法,所以這個效果不一定是MJRefresh實現(xiàn)的,沒有實現(xiàn)的小伙伴就不要在MJRefresh中苦苦尋找了。
功能實現(xiàn)
實現(xiàn)方法很簡單,需要用到tableView的一個代理方法,就可輕松實現(xiàn)。- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath就是這個方法,自定義顯示cell。這個方法不太常用。但是這個方法可在每個cell將要第一次出現(xiàn)的時候觸發(fā)。然后我們可設置當前頁面第幾個cell將要出現(xiàn)時,觸發(fā)請求加載更多數(shù)據(jù)。
具體代碼
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger row = [indexPath row];
if (row == self.dataArray.count - 2 && self.isfinish) {
//dataArray是存放數(shù)據(jù)的數(shù)組,isfinish是請求是否完成的標識
self.pageNum++;//第幾頁
[self.updataDic addEntriesFromDictionary:@{@"pageSize": @(10), @"pageNum" :@(self.pageNum)}];//請求參數(shù)
[self setupDataModel];//具體請求
}
}
-(void)serverApi_FinishedSuccessed:(APIRequest *)api result:(APIResult *)sr
{//網(wǎng)絡請求成功代理方法
if (api == self.goodsAPIRequest) {
if (self.goodsAPIRequest.netWorkType == 22) {
self.dataModel = [[GoodsListModelBase alloc]initWithDictionary:sr.dic];//轉(zhuǎn)化model
[self.dataArray addObjectsFromArray:self.dataModel.data];
if (self.dataModel.data.count == 0) {
[self.tableView.mj_footer endRefreshingWithNoMoreData];
self.isfinish = NO;
}else {
[self.tableView reloadData];
if (@available(iOS 11,*)) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//iOS11之后reloadData方法會執(zhí)行- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 方法,將當前所有的cell過一遍,而iOS11之前只是將展示的cell過一遍。故加此方法使其在過第一次的時候不執(zhí)行加載更多數(shù)據(jù)
self.isfinish = YES;
});
}else {
self.isfinish = YES;
}
}
}
}
效果如下
流暢.gif
是不是很流暢。當然還得配上MJRefresh下拉加載,以防網(wǎng)絡狀態(tài)不好的情況下刷不出數(shù)據(jù)。
關(guān)于加載時抖動問題可加上
self.tableView.estimatedRowHeight = 0;
self.tableView.estimatedSectionHeaderHeight = 0;
self.tableView.estimatedSectionFooterHeight = 0;
關(guān)閉預估算高度.