欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS列表上拉(平滑加載數(shù)據(jù))自動加載數(shù)據(jù)的問題解決

 更新時間:2021年07月12日 11:17:24   作者:雨灑瀟湘  
這篇文章主要給大家介紹了關于iOS列表上拉(平滑加載數(shù)據(jù))自動加載數(shù)據(jù)問題的相關資料,本文實現(xiàn)的效果很多app都用的這種效果,文中通過圖文以及實例代碼介紹的非常詳細,需要的朋友可以參考下

項目需求

我的的列表需要改變,原來的分頁加載采用的是MJRefresh框架進行加載更多數(shù)據(jù),這需要有一個上拉動作才能觸發(fā),而我的產(chǎn)品的意思是當快要滑動到底部時自動加載下一頁數(shù)據(jù)。我自己看了一下,發(fā)現(xiàn)很多app都是采用這種模式。

關于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];//轉化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ù)。

關于加載時抖動問題可加上

    self.tableView.estimatedRowHeight = 0;
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;

關閉預估算高度.

總結

到此這篇關于iOS列表上拉(平滑加載數(shù)據(jù))自動加載數(shù)據(jù)問題解決的文章就介紹到這了,更多相關iOS列表上拉自動加載數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論