我在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;
}
我的单元格未显示视图知道为什么吗?
从文件instantiateViewControllerWithIdentifier:
-
每次调用时,此方法都会创建一个指定视图控制器的新实例。
您最好存储一个UIViewController
s数组,以便可以重复使用它们,而不是存储ID并在每次tableView:cellForItemAtIndexPath:
调用时实例化一个新的视图控制器。
另外,由于UICollectionViewCell
s被重用,因此每次tableView:cellForItemAtIndexPath:
调用都向该单元格添加子视图是不明智的。相反,请考虑UICollectionViewCell
使用mainView属性创建自己的子类。setMainView:
在设置新框架(或添加布局约束)并将其添加为子视图之前,您可以覆盖从其主视图中删除旧的mainView。
同样,您应该实现适当的UIViewController包含的方法。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句