【发布时间】:2020-04-08 23:16:10
【问题描述】:
我已经实现了我自己的集合视图布局,它是 UICollectionViewLayout 的子类。在prepare 中,我计算并缓存布局属性。
在layoutAttributesForElementsInRect 中,我过滤掉那些超出矩形的:
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
return itemAttributesCache.filter { $0.frame.intersects(rect) }
}
我希望为layoutAttributesForElementsInRect中返回的每个索引路径调用数据源方法func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
但是我发现如果我只返回这样的整个属性集:
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
return itemAttributesCache
}
cellForItemAt 方法仍然只为可见矩形内的属性调用。
这是预期的行为吗?如果是的话,在我的代码中过滤属性的目的是什么,如果集合视图本身可以做到这一点?我没有发现任何性能问题。
是否可以强制collectionview为所有返回的属性调用cellForItemAt?
【问题讨论】:
-
如果你还没有看过这个 WWDC 演讲,我强烈推荐它,他们在这里解释了很多你不清楚的地方。 developer.apple.com/videos/play/wwdc2018/225
-
@OscarApeland 在那次谈话中没有回答我的问题
标签: ios objective-c swift uicollectionview uicollectionviewlayout