【发布时间】:2017-11-02 05:29:54
【问题描述】:
我有一个 UICollectionView,它有 2k+ 条记录。在任何时候都只会看到 6-7 行,并且我正在使用可重复使用的单元格。一切正常。
但是当我在 cellForRowAtIndexPath 中创建控制台日志时,最初会加载 80 多行。之后,它再次加载第 1 7 行,并且仅正确加载可见单元格。
为什么最初加载 80 多行。这会使初始加载稍有延迟。
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return results.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
print("Cell For Row -> \(indexPath)")
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ResultCell", for: indexPath) as! ResultCell
cell.data = result[indexPath.item]
return cell
}
更新:
我使用的是自动调整单元格大小。所以我确实将 estimatedItemSize 定义为 UICollectionViewFlowLayoutAutomaticSize
我尝试定义一个合适的 CGSize 而不是 UICollectionViewFlowLayoutAutomaticSize。额外的单元格停止加载。
【问题讨论】:
-
这很奇怪。你能创建一个演示项目并给我链接吗?
-
你在用
prefetchDataSource吗? -
@Jack 是的.. 我愿意.. 但我删除了它并尝试了。还是一样的
标签: ios swift uicollectionview uicollectionviewcell