iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >IOS 列表tableView、collectionView上拉自动加载数据,预加载效果
  • 77
分享到

IOS 列表tableView、collectionView上拉自动加载数据,预加载效果

iOS基础iosobjective-cxcode 2022-06-08 17:06:27 77人浏览 佚名
摘要

列表tableView、collectionView上拉自动加载数据,预加载效果,废话不多说直接上代码: 方法一:直接用MJRefresh就可以实现MJRefreshAutoNORMalFooter *footer =&

列表tableView、collectionView上拉自动加载数据,预加载效果,废话不多说直接上代码:
方法一:直接用MJRefresh就可以实现

MJRefreshAutoNORMalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        //请求数据
        weakSelf.pageNo++;
        [weakSelf requestListData];
    }];
    //自动触发时间,如果为 -1, 则为无限触发
    footer.autoTriggerTimes = -1;
    //当底部控件出现多少时就自动刷新(默认为1.0,也就是底部控件完全出现时,才会自动刷新,设置为-1则是未到满屏的时候刷新
    footer.triggerAutomaticallyRefreshPercent = -1;
    self.tableView.mj_footer = footer;

方法二:通过数据条数来控制

//模拟请求网络数据
     WEAKSELF;
    self.isLoading = YES;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        sleep(1.0);
        NSMutableArray *arr = [NSMutableArray array];
        for (int i = 0; i<self.pageSize; i++) {
            NSString *str = @"哈哈哈哈哈";
            [arr addObject:str];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            if (weakSelf.pageNo == 1 && weakSelf.allData.count) { //清空
                [weakSelf.allData removeAllObjects];
            }
            self.isLoading = NO;
            //[weakSelf diSSMisshud];
            [weakSelf.allData addObjectsFromArray:arr];
            [weakSelf.tableView reloadData];
        });
    });

然后在代理方法里判断数据条数进行刷新:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.isLoading == YES) {
        return;
    }
    // 80%数据出现后,就去加载数据
    if (indexPath.row > self.allData.count * 0.8) {
        self.pageNo++;
        [self requestListData];
    }
    
}

方法三:与第二种方法类似:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == self.allData.count - 5 && self.isLoading) {
        self.pageNo++;
        [self requestListData];
    }
}

附上demo,喜欢的点赞

END.

--结束END--

本文标题: IOS 列表tableView、collectionView上拉自动加载数据,预加载效果

本文链接: https://www.lsjlt.com/news/30812.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作