vikzilla
vikzilla

Reputation: 4138

Load UICollectionView cells without reuse

I have a collectionView that will only display three cells. Each cell is of the same type of custom collectionViewCell that I've set up in storyboard. I only want each cell instantiated once and only once; not reused.

How can I return cells from cellForItemAtIndexPath: datasource method that aren't reused? Is it possible to do this?

Upvotes: 1

Views: 6520

Answers (2)

ChrisHaze
ChrisHaze

Reputation: 2810

Reusing a UICollectionViewCell doesn't mean you're getting rid of the previous ones that are no longer on the screen.

It's a structured process for memory efficiency and here's how it works:

  1. There are a distinct number of cells automatically generated by the UICollectionView based on the UICollectionViewLayout you're needing.
  2. These cells are built upon a UIScrollView that reuses them instead of allocating memory for every cell that will eventually be appearing.
  3. imagine the dataset being a "wheel" and it's size dependent on how many cells you're needing to create.
  4. Let's say your "wheel" has a total of 100 different "views" that will populate the cells
  5. If the screen can only show 5 at a time, it wouldn't make sense to load all 100... Just the 5 that will be within the view and the ones preparing to enter the screen as you're scrolling..
  6. By Using the UICollectionViewDataSource Protocols, your collection view repopulates the cell that just left the screen and puts it below, or above, the displayed list to be "reused" instead of causing memory problems.

Collection Views are incredibly efficient and I would recommend utilizing their built in Memory saving dequeuereusablecellwithidentifier.

Now, to answer your question:

You would get the result you're wanting by putting the Necessary Data you'll need within the cells (possibly making a subclass of the UICollectionViewCell if needed) inside a NSArray.

You'll access each object (1 - 100 in our example) using the cellForItemAtIndexPath and as your scroll, the basic principle of reusing the cells will display the NSArray objects that are needed to be displayed.

I'm hoping this makes sense and it helps you solve your problem!

Upvotes: 3

Onik IV
Onik IV

Reputation: 5047

Use properties:

@property (nonatomic, strong) UICollectionViewCell *firstCell;
@property (nonatomic, strong) UICollectionViewCell *secondCell;
@property (nonatomic, strong) UICollectionViewCell *thirdCell;

In viewDidLoad you can create it:

 self.firstCell = [[UICollectionViewCell alloc] init];
// Do you need...

And your delegate methods, look like:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.row) {
    case 0:
        return self.firstCell;
        break;
    case 1:
        return self.secondCell;
        break;
    case 2:
        return self.thirdCell;
        break;

    default:
        return nil;
        break;
}

}

Upvotes: -2

Related Questions