I noticed that UICollectionView
calls collectionView:cellForItemAtIndexPath:
on its data source quite a few times. For example, on each layoutSubviews
all cells are "reconfigured", no matter if they were already visible or not.
When collectionView:cellForItemAtIndexPath:
is called, we're expected to:
Your implementation of this method is responsible for creating, configuring, and returning the appropriate cell for the given item. You do this by calling the
dequeueReusableCellWithReuseIdentifier:forIndexPath:
method of the collection view and passing the reuse identifier that corresponds to the cell type you want. That method always returns a valid cell object. Upon receiving the cell, you should set any properties that correspond to the data of the corresponding item, perform any additional needed configuration, and return the cell.
The problem is that configuring a cell is not always a cheap operation, and I don't see why I should reconfigure cells that are already configured.
How can we avoid redundant configuration of the cells? Or is there something I'm not understanding correctly?
I don't think you can avoid this. The problem is that UICollectionView is so general and FlowLayout isn't the only layout. Since you are allowed to make crazy layouts, any layout change, like a layoutsubviews, could completely change the layout you want -- a grid in portrait and a triangular arrangement in landscape... The only way to know what the layout should be is to find out the location and size of each cell.
UICollection view is not cheap for lots and lots of elements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With