【问题标题】:how to do pagination when tableview at top当tableview在顶部时如何进行分页
【发布时间】:2017-06-23 05:37:44
【问题描述】:

我想在用户滚动tableView 到顶部时进行分页,因为我使用了这个UIScrollView 方法。但它会多次调用api

【问题讨论】:

  • top 表示您的 tableview 显示最后一个单元格的时间?
  • 不,第一次我会得到 10 条记录,当我滚动到顶部以显示旧 cmets 时。
  • @KKRocks,你能建议我避免在数组中重复插入数据吗?
  • repeat 表示你是否多次调用同一个api?
  • 是的,当我滚动 tableview 时它会调用多次。

标签: ios uitableview


【解决方案1】:

试试这个

当您的服务器提供数据计数时

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) { // indexpath.row == 0 means top cell in tableViewCell
        if (arrNews.count  < totalPageCount  ) { //totalPageCount means total number of data exist in server which is needs in server resopnse
            [self getDataFromServer];
        }
    }
}

当您的服务器未提供数据计数时

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) { // indexpath.row == 0 means top cell in tableViewCell
        if (isMoreData) { //isMoreData is bolean you need to disable while not data found from server
            [self getDataFromServer];
        }
    }
}

【讨论】:

  • 什么是总页数?
  • 我已经解释过它的服务器数据计数表示货币数据如何保留在服务器中。
  • 来自服务器响应,当我调用 api 时,我得到了 10 条记录,我没有得到有多少剩余数据?每次顶部的tableview我再次调用api来获取whatsapp之类的新记录。
  • 你需要这个数字,因为我们怎么知道服务器没有更多的数据存在。
  • 我传递了一个参数 page_id ,当我得到响应时它会增加,当我再次调用 api 时它会调用增加 page_id 所以如果数据不可用,它将不会返回。
【解决方案2】:

你可以通过scrollViewDidScroll中的contentOffset查看

如果内容偏移 y 位置

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.y <= 20 && !reachedEndOfItems) {
        currentOffset = self.wallProfileTableView.contentOffset;
        [self loadMoreMessage];
    }
}

这将类似于我们在向下滚动时加载聊天

【讨论】:

  • 达到了什么EndOfItems?
  • 它是一个标志,当您加载所有项目时,如果您每页调用 10 个项目,并且在 1 个调用中您收到不超过 10 条记录,这意味着您拥有所有数据,不需要更多分页
猜你喜欢
  • 2022-01-09
  • 1970-01-01
  • 2017-10-25
  • 1970-01-01
  • 2011-02-17
  • 1970-01-01
  • 2017-04-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多