[英]How to cache UIViewControllers for UICollectionViewCell use?
[英]UiviewControllers inside UICollectionViewCell
我在UIViewController中有一个UICollectionView。 collectionView包含来自故事板的8个视图控制器标识符的数组。 数组中的每个视图控制器都继承自保存collectionView的UIViewController。
这是代码:
- (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;
}
所以我的问题是我的集合视图滑动性能非常慢。 如何改进UICollectionView的性能? 我的视图控制器是否在显示时被“杀死”?
编辑:
在这里的建议后,我已经将我的数组更改为持有视图控制器而不是标识符nsstring。
在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;
}
我的单元格没有显示视图任何想法为什么?
通过使用队列来保持和重用一定数量的实例,我能够获得包含每个子视图控制器的单元集合的平滑滚动性能。
GitHub列出了许多重用队列实现; 并且,在对几乎每一个人进行相当彻底的审查之后,我可以自信地推荐这个:
https://github.com/acoomans/ACReuseQueue
根据自述文件:重用队列是在对象分配和初始化耗时时快速重用对象的一种方法。
您的视图未显示的原因是您没有从单元子类中实例化子视图控制器,也没有从cellForItemAtIndexPath中添加视图。 这是关于范围的,你已经倒退了。
这里提供了向子单元添加子视图控制器的完美说明:
从instantiateViewControllerWithIdentifier:
的文档instantiateViewControllerWithIdentifier:
-
每次调用时,此方法都会创建指定视图控制器的新实例
你最好存储一个UIViewController
数组,这样它们就可以被重用,而不是每次调用tableView:cellForItemAtIndexPath:
都存储ID并实例化一个新的视图控制器。
此外,由于重新使用了UICollectionViewCell
,因此每次调用tableView:cellForItemAtIndexPath:
时,继续向单元格添加子视图是tableView:cellForItemAtIndexPath:
。 相反,请考虑使用mainView属性创建自己的UICollectionViewCell
子类。 在设置新的框架(或添加布局约束)并将其添加为子视图之前,您可以覆盖setMainView:
从其超级视图中删除旧的mainView。
您还应该实现适当的UIViewController包含的方法。
使用Swift更新(在collectionview的单元格中添加viewcontrollers)
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
let cellContentVC = self.storyboard?.instantiateViewController(withIdentifier: screens[indexPath.row])
cellContentVC?.view.frame = cell.bounds
cell.addSubview(cellContentVC!.view)
return cell
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.