[英]UICollectionView is calling `sizeForItem` for all cells ahead of time?
我正在做一个无限滚动的代码,并且遇到了这个问题, UICollectionView
将在加载之前为每个单元sizeForItem
。 我不明白为什么会这样做,因为在给定的时间只显示了一定数量的数字
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
print("sizeForItem\(Date())")
let height = collectionView.frame.height
guard tickerItems.count > 0 else {
return CGSize(width: 500, height: 250)
}
let tickerCell = collectionView.cellForItem(at: indexPath) as? TickerCell ?? TickerCell()
let tickerItem = tickerItems[indexPath.row % tickerItems.count]
let width = tickerCell.width(for: tickerItem)
return CGSize(width: width, height: height)
}
我发生了一些事情:也许我返回的宽度很小? 不,我检查了它们是否正常。 是否启用了预取? 不,我将其禁用。
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return tickerItems.count > 0 ? 100000 : 0
}
即使禁用了控制自动滚动的计时器,我仍然遇到相同的问题。 即使该索引长时间没有显示,collectionView也会计算每个索引的大小。 即,它将在最终取消阻止加载collectionView的UI之前按字面量计算100,000个索引的大小。
集合视图需要知道它将有多大,因此需要获取所有单元格的大小。 集合视图是滚动视图。 它需要知道其内容大小,并且只能通过询问您所有单元的大小来确定它的大小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.