列表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];
}
}
END.
--结束END--
本文标题: IOS 列表tableView、collectionView上拉自动加载数据,预加载效果
本文链接: https://www.lsjlt.com/news/30812.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0