iOS列表上拉(平滑加載數(shù)據(jù))自動加載數(shù)據(jù)的問題解決
我的的列表需要改變,原來的分頁加載采用的是MJRefresh框架進(jìn)行加載更多數(shù)據(jù),這需要有一個(gè)上拉動作才能觸發(fā),而我的產(chǎn)品的意思是當(dāng)快要滑動到底部時(shí)自動加載下一頁數(shù)據(jù)。我自己看了一下,發(fā)現(xiàn)很多app都是采用這種模式。
關(guān)于MJRefreshMJRefresh中并沒有這樣的方法,所以這個(gè)效果不一定是MJRefresh實(shí)現(xiàn)的,沒有實(shí)現(xiàn)的小伙伴就不要在MJRefresh中苦苦尋找了。
功能實(shí)現(xiàn)實(shí)現(xiàn)方法很簡單,需要用到tableView的一個(gè)代理方法,就可輕松實(shí)現(xiàn)。- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath就是這個(gè)方法,自定義顯示cell。這個(gè)方法不太常用。但是這個(gè)方法可在每個(gè)cell將要第一次出現(xiàn)的時(shí)候觸發(fā)。然后我們可設(shè)置當(dāng)前頁面第幾個(gè)cell將要出現(xiàn)時(shí),觸發(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過一遍。故加此方法使其在過第一次的時(shí)候不執(zhí)行加載更多數(shù)據(jù)self.isfinish = YES; });}else { self.isfinish = YES;} } }}效果如下
流暢.gif
是不是很流暢。當(dāng)然還得配上MJRefresh下拉加載,以防網(wǎng)絡(luò)狀態(tài)不好的情況下刷不出數(shù)據(jù)。
關(guān)于加載時(shí)抖動問題可加上
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)文章:
1. php模擬實(shí)現(xiàn)斗地主發(fā)牌2. 理解PHP5中static和const關(guān)鍵字3. jQuery 實(shí)現(xiàn)DOM元素拖拽交換位置的實(shí)例代碼4. Java如何基于反射機(jī)制獲取不同的類5. IntelliJ IDEA安裝插件的方法步驟6. Android table布局開發(fā)實(shí)現(xiàn)簡單計(jì)算器7. MyBatis中的JdbcType映射使用詳解8. Android 在 res/layout 文件夾 下創(chuàng)建一個(gè) 子文件夾實(shí)例9. Python random庫使用方法及異常處理方案10. .Net Core使用Coravel實(shí)現(xiàn)任務(wù)調(diào)度的完整步驟

網(wǎng)公網(wǎng)安備