在UITableView中设置的App分页

时间:2018-04-25 08:40:00

标签: objective-c uitableview pagination

我已经通过应用程序端的编码设置了分页,所以我只从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 Count并且我的应用程序崩溃了。

所以请分享你的建议,以避免崩溃。我已经应用了25个分页限制,所以每次添加到arraylist后25个项目都应该调用getMore25Data,直到offset小于arrrMainList。

1 个答案:

答案 0 :(得分:1)

在我看来,您应该添加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