I have implemented my own collection view layout subclassed from UICollectionViewLayout. In prepare i calculate and cache layout attributes.
In layoutAttributesForElementsInRect i filter out the ones that are out of rect:
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
return itemAttributesCache.filter { $0.frame.intersects(rect) }
}
I expected that data source method func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell will be called for every index path that has been returned in layoutAttributesForElementsInRect
However i found if i just return the entire set of attributes like that:
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
return itemAttributesCache
}
cellForItemAt method is still called only for attributes inside visible rect.
Is it the expected behavior? And if yes what is the purpose to filter attributes in my code, if collection view does it itself? I did not find any performance issues with it.
Is it possible to force collectionview to call cellForItemAt for all returned attributes?