【发布时间】:2018-06-16 14:53:48
【问题描述】:
我使用the idea of custom TableView pagination 使用 willDisplayCell(或 cellForRowAt)并从服务器更新。
问题是即使对于不在屏幕上的单元格也会调用 willDisplay。
如何处理这种行为并更改流程以仅在用户滚动到最后一个单元格时更新单元格?
private func isLoadingIndexPath(_ indexPath: IndexPath) -> Bool {
return indexPath.row == self.orders.count - 1
}
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
// fetch new data if user scroll to the last cell
guard isLoadingIndexPath(indexPath) else { return }
if self.totalItems > orders.count {
fetchNextPage()
}
}
private func fetchNextPage() {
self.currentPage += 1
self.delegate?.didReachLastCell(page: currentPage)
}
didReachLastCell 调用 addOrders:
func addOrders(_ orders: [OrderView]) {
self.orders.append(contentsOf: orders)
reloadOrders()
}
fileprivate func reloadOrders() {
self.tableView.reloadData()
}
注意:同样的问题对于 cellForRowAt 方法也可以重现。
【问题讨论】:
标签: ios swift uitableview pagination