1

我在UITableViewDataSourcePrefetching实现中面临一个问题。

我在云中总共有 62 条记录,我正在调用每页返回 15 条记录的 API。

我也实现了prefetchRowsAt方法,该方法对我来说部分效果很好。

问题是上面的方法没有返回它要加载的所有 indexPath,所以你可以从下面的日志中看到它只上升到 53,在我的表格视图中它只显示 60 条记录。

Indexpaths to fetch : [[3, 51], [3, 42], [3, 52], [3, 41], [3, 53], [3, 40], [3, 54], [3, 39], [3, 55], [3, 38], [3, 56], [3, 37], [3, 57], [3, 36], [3, 58], [3, 35], [3, 59], [3, 34], [3, 60], [3, 33]]
Indexpaths to fetch : [[3, 51], [3, 50], [3, 49], [3, 48], [3, 47], [3, 46], [3, 45], [3, 44], [3, 43]]
Indexpaths to fetch : [[3, 52]]
Indexpaths to fetch : [[3, 53]]

这是我的方法实现

extension MyViewController:UITableViewDataSourcePrefetching{
    func tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath]) {
        #if DEBUG
            print("Indexpaths to fetch : \(indexPaths)")
        #endif

        if indexPaths.contains(where: isLoadingCell) {
            self.loadFurthersSessionNotification?()
        }
    }
}

private extension MyViewController {
  func isLoadingCell(for indexPath: IndexPath) -> Bool {
    if indexPath.section != 3{
        return false
    }
    return (indexPath.row - 1) >= self.aryCloudObjects.count
  }
}

任何帮助将不胜感激。

谢谢

4

1 回答 1

0

您所看到的行为没有什么意外的。不能保证prefetchRowsAt会为表中的每一行调用。

文档中:

表格视图不会立即为需要的单元格调用此方法,因此您的数据源对象也必须能够获取数据本身。

预取允许您准备好显示行,但是如果在cellForRowAt:调用时数据没有被预取,那么您需要获取数据。

于 2020-01-24T03:00:30.343 回答