Just curious, is it possible to disable the reuse functionality on UICollectionview
?
I have a limited amount of cells, which may vary, but the reinitialization of the cell might be a little heavy and I'm better off not reusing them.
Trying to initialize the cell without dequeueReusableCellWithReuseIdentifier
I get the exception:
NSInternalInconsistencyException', reason: 'the view returned from collectionView:cellForItemAtIndexPath: was not retrieved by calling -dequeueReusableCellWithReuseIdentifier:forIndexPath.
If you're working entirely in code, you can register a UICollectionViewCell subclass for use with your collection view, so that new cells are dequeued and re-use automatically by the system. If a cell doesn't already exist that can be re-used, a new one will be created automatically.
From apple's documentation, UICollectionView is: An object that manages an ordered collection of data items and presents them using customizable layouts. The name and definition makes it clear, it is a way to display a Collection of UI Views in our app. The individual view is referred as a Cell.
Select the Main storyboard from the file browser. Add a CollectionView by pressing command shift L to open the storyboard widget window. Drag the collectionView onto the main view controller. Add constraints to the UICollectionView widget to ensure that the widget fills the screen on all devices.
the reinitialization of the cell might be a little heavy
It's unlikely that resetting the contents of a cell will be more expensive than creating a new one -- the whole point of cell reuse is to improve performance by avoiding the need to constantly create new cells.
Trying to initialize the cell without dequeueReusableCellWithReuseIdentifier I get the exception:
I'd take that as a strong indication that the answer to your question is no. Further, the documentation says:
...the collection view requires that you always dequeue views, rather than create them explicitly in your code.
So again, no.
To disable cells reuse just dequeue your cell with a specific identifier for that cell index path, and register that identifier before hand.
- (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];
// ...
// ...
// ...
}
Notice that reusing isn't completely disabled in the above method since there's an identifier for each cell, which is what everybody will probably need, but if you need to completely disable cells reusability 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'm just answering the question here, this is totally not recommended, specially that second method.
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