UICollectionView without reusing cells

Curious is it possible to disable the reuse feature on a UICollectionview ? I have a limited number of cells that can change, but reinitializing the cell can be a little difficult, and I better not reuse them. Trying to initialize a cell without dequeueReusableCellWithReuseIdentifier I get an exception:

NSInternalInconsistencyException ', reason:' the view returned from the collection View: cellForItemAtIndexPath: was not obtained by calling -dequeueReusableCellWithReuseIdentifier: forIndexPath.

+8
ios objective-c cocoa-touch
source share
2 answers

reinitializing a cell can be a little difficult

It is unlikely that dumping the contents of a cell will be more expensive than creating a new one - the whole point of reusing cells should increase productivity, avoiding the need to constantly create new cells.

Trying to initialize a cell without dequeueReusableCellWithReuseIdentifier I get an exception:

I would take this as convincing evidence that there is no answer to your question. In addition, the documentation says :

... a collection view requires that you always deactivate the views, rather than creating them explicitly in your code.

So again no .

+7
source share

To disable cell reuse, simply delete the cell with a specific identifier for this cell pointer path and register that identifier before starting work.

 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { NSString *identifier = [NSString stringWithFormat:@"Identifier_%d-%d-%d", (int)indexPath.section, (int)indexPath.row, (int)indexPath.item]; [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:identifier]; UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; // ... // ... // ... } 

Please note that reuse is not completely disabled in the above method, since for each cell there is an identifier that everyone will probably need. But if you need to completely disable cell reuse, you can do the following.

 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { static int counter = 0; NSString *identifier = [NSString stringWithFormat:@"Identifier_%d", counter]; [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:identifier]; UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; counter++; // ... // ... // ... } 

IMPORTANT : I just answer this question here, it is completely not recommended , especially this second method. I use this first method in the case where I have multidirectional scrolling, and there are some delegate methods regarding focusing on tvOS that I need to call for each cell.

+4
source share

All Articles