UiviewControllers inside UICollectionViewCell

I have a UICollectionView inside a UIViewController. TheView collection contains an array of 8 IDs for the view controllers from the storyboard. Each view controller in an array inherited from UIViewController that contains a CollectionView.

Here is the code:

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return _contentPageRestorationIDs.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

BaseContentViewController *contentViewController = (BaseContentViewController *)[self.storyboard instantiateViewControllerWithIdentifier:self.contentPageRestorationIDs[indexPath.row]];

// Set any data needed by the VC here
contentViewController.rootViewController = self;
contentViewController.view.frame = cell.bounds;
[cell addSubview:contentViewController.view];

return cell;
}

      

So my problem is that the performance of a collection in a collection is very slow. How can I improve the performance of the UICollectionView? Are my view controllers inside the cell being "killed" when not showing?

Edit:

after consulting here, i changed my array to have view controllers instead of nsstring ids.

In ViewDidLoad:

_collectionView.delegate=self;
_collectionView.dataSource=self;
PRzeroVC  *zeroVC    = [[PRzeroVC alloc]init];
PRoneVC   *oneVC     = [[PRoneVC alloc]init];
PRtwoVC   *twoVC     = [[PRtwoVC alloc]init];
PRthreeVC *threeVC   = [[PRthreeVC alloc]init];
PRfourVC  *fourVC    = [[PRfourVC alloc]init];
PRfiveVC  *fiveVC    = [[PRfiveVC alloc]init];

_base           = [[BaseContentViewController alloc]init];

_pages = [[NSArray alloc]initWithObjects:zeroVC,oneVC,twoVC,threeVC,fourVC,fiveVC, nil];

[_collectionView reloadData];



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = (UICollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
_base = (BaseContentViewController *)_pages[indexPath.row];

_base.rootViewController = self;
_base.view.frame = cell.bounds;
[cell addSubview:_base.view];

return cell;
}

      

My cell is not displaying a view. Any idea why?

+3


source to share


2 answers


From the documentation instantiateViewControllerWithIdentifier:

-

This method creates a new instance of the specified view controller each time it is called

Better to keep the array UIViewController

so that they can be reused instead of storing IDs and creating a new view controller every time it is called tableView:cellForItemAtIndexPath:

.



Also, since it is UICollectionViewCell

reusable, you shouldn't constantly add the subview to the cell every time it is called tableView:cellForItemAtIndexPath:

. UICollectionViewCell

Consider creating your own subclass with the mainView property instead. You can override setMainView:

to remove the old mainView from your supervisor before setting a new one frame (or adding layout constraints) and adding it as a subview.

Also you must implement methods for the proper UIViewController> .

+2


source


I was able to get a smooth performance scrolling of a collection of cells containing each their own child view controller, using a queue to save and reuse a set number of instances.

GitHub lists several reuse queue implementations; and, after thorough enough analysis of almost everyone if they are, I can safely recommend this one:

https://github.com/acoomans/ACReuseQueue

In ReadMe: Reuse Queue is a way to quickly reuse objects when object allocation and initialization are time consuming.



The reason your view is not showing is because you are not instantiating the child view controller from a subclass of the cell and adding the view from within cellForItemAtIndexPath. It's about scope and you got it back.

Here's a complete set of instructions for adding child view controllers to cells:

http://khanlou.com/2015/04/view-controllers-in-cells/

+3


source







All Articles