在 UITableView 中设置的应用分页中



我通过应用程序端的编码设置了分页,所以我只从 api 获取所有数据一次,而不是将分页设置为波纹管方法

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView == self.tblForList)
{
if (indexPath.row == arrForList.count - 1 && arrForList.count < arrForMainList.count)
{
offset += limit;
page++;
[self getMore25DataFromMainAry];
}
}
}

这里的问题是,如果用户如此快速地滚动多次,但 UITableView 未成功重新加载方法被多次调用,因此我的偏移量增加到超过 MainArray 计数并且我的应用程序崩溃。

因此,请分享您的建议以避免崩溃。在这里,我应用了 25 个分页限制,因此每次在 25 个项目添加到 arraylist getMore25Data 后都应该调用,直到偏移量小于 arrrMainList。

在我看来,你应该添加一个BOOL属性,使getMore25DataFromMainAry在运行时无法调用。

@interface YourClass ()
@property(nonatomic, assign) BOOL loading;
@end
@implementation YourClass
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if(tableView == self.tblForList) {
if (indexPath.row == arrForList.count - 1 && arrForList.count < arrForMainList.count) {
[self getMore25DataFromMainAry];
}
}
}
- (void)getMore25DataFromMainAry {
if (self.loading) {
// Don't do anything until loading more completely
return;
}
// Start loading more
self.loading = YES;
offset += limit;
page++;
// Do whatever you want to load more data.
// After receiving new data, set |self.loading| to NO
self.loading = NO;
}
@end

最新更新