Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionView cell gets configured too many times

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?

like image 234
hpique Avatar asked Mar 20 '13 12:03

hpique


1 Answers

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.

like image 97
Charlie Price Avatar answered Oct 11 '22 14:10

Charlie Price