iOS列表上拉(平滑加載數(shù)據(jù))自動加載數(shù)據(jù)的問題解決
我的的列表需要改變,原來的分頁加載采用的是MJRefresh框架進行加載更多數(shù)據(jù),這需要有一個上拉動作才能觸發(fā),而我的產(chǎn)品的意思是當(dāng)快要滑動到底部時自動加載下一頁數(shù)據(jù)。我自己看了一下,發(fā)現(xiàn)很多app都是采用這種模式。
關(guān)于MJRefreshMJRefresh中并沒有這樣的方法,所以這個效果不一定是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ā)。然后我們可設(shè)置當(dāng)前頁面第幾個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是請求是否完成的標(biāo)識self.pageNum++;//第幾頁[self.updataDic addEntriesFromDictionary:@{@'pageSize': @(10), @'pageNum' :@(self.pageNum)}];//請求參數(shù)[self setupDataModel];//具體請求 }}-(void)serverApi_FinishedSuccessed:(APIRequest *)api result:(APIResult *)sr{//網(wǎng)絡(luò)請求成功代理方法 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 方法,將當(dāng)前所有的cell過一遍,而iOS11之前只是將展示的cell過一遍。故加此方法使其在過第一次的時候不執(zhí)行加載更多數(shù)據(jù)self.isfinish = YES; });}else { self.isfinish = YES;} } }}效果如下
流暢.gif
是不是很流暢。當(dāng)然還得配上MJRefresh下拉加載,以防網(wǎng)絡(luò)狀態(tài)不好的情況下刷不出數(shù)據(jù)。
關(guān)于加載時抖動問題可加上
self.tableView.estimatedRowHeight = 0; self.tableView.estimatedSectionHeaderHeight = 0; self.tableView.estimatedSectionFooterHeight = 0;
關(guān)閉預(yù)估算高度.
總結(jié)到此這篇關(guān)于iOS列表上拉(平滑加載數(shù)據(jù))自動加載數(shù)據(jù)問題解決的文章就介紹到這了,更多相關(guān)iOS列表上拉自動加載數(shù)據(jù)內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
