Prefill UICollectionView Cell Reuse Queue

Problem

I have an application with one UICollectionView that stutters the first time I scroll through it. I narrowed down the source to the fact that new cells (2) are created (using initWithFrame: , because there are no cells that can be reused. After this initial scroll, the reuse queue is not empty, and the cells can be reused, and there is no stuttering.

The hack

So, I was able to solve the problem using a private method in iOS runtime headers:

 - (void)_reuseCell:(id)arg1; 

My guess is that this is where cells are added back to the reuse queue. The hack to use this undocumented method is as follows:

 int prefillCellCount = 10; NSMutableArray *cells = [[NSMutableArray alloc] initWithCapacity:prefillCellCount]; for (int i=0; i<10prefillCellCount i++) { UICollectionViewCell *cell = [self.gridView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:[NSIndexPath indexPathForRow:i inSection:0]]; [cells addObject:cell]; } for (UICollectionViewCell *cell in cells) { [self.gridView _reuseCell:cell]; } 

Failure is working! There is no stutter when scrolling.

Question

I don’t think I can get past App Store reviewers with this hack. Is there a way to do this without using undocumented methods? If not, is this my only resort to just confuse the hack?

+6
source share
1 answer

Is there a way to do this without using undocumented methods?

If the problem is that it takes a long time to create cells, you can simply create a few extra functions and hold them in your view controller until the collection asks for them. First select a few cells, as you do in your code, and call dequeueReusableCellWithReuseIdentifier: to collect a collection to create them. Store them in an array in the view controller. Next, in your method ...cellForItemAtIndexPath: if the array is not empty, remove the cell from the array and use it instead of querying the collection for the reused cell.

This should solve your problem by moving the point at which cells are created without using undocumented methods.

An alternative approach is to speed up the process of creating a cell, so you don’t have to worry about pre-distributing the cells. Profile your code to see why your cells take so long to create. Perhaps you can simplify the structure of your cell by removing unnecessary views?

+4
source

All Articles