【发布时间】:2015-08-03 06:32:18
【问题描述】:
我在 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;
}
我的单元格没有显示视图知道为什么吗?
【问题讨论】:
-
你想达到什么目的?你可以用
UIPageViewController或子视图控制器来做到这一点吗?
标签: ios objective-c uiviewcontroller uicollectionview uicollectionviewcell