在UITableViewDataSourcePrefetching实现中,我面临一个问题。
我在云中总共有62条记录,我正在调用API,它每页返回15条记录。
我还实现了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
}
}任何帮助都将不胜感激。
谢谢
发布于 2020-01-24 03:00:30
你所看到的行为没有什么出乎意料的。不能保证对表中的每一行都调用prefetchRowsAt。
来自文档
表视图不会立即对所需的单元格调用此方法,因此数据源对象也必须能够获取数据本身。
预取允许您准备显示行,但是如果在调用cellForRowAt:时还没有预取数据,那么您就需要获取数据。
https://stackoverflow.com/questions/59889734
复制相似问题