在UICollectionViewCell中包含UIViewControllers

4

我有一个UICollectionView嵌套在一个UIViewController中。 collectionView包含了一个由8个控制器标识符组成的数组,这些控制器来自于storyboard。 数组中的每个控制器都继承自持有UICollectionView的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 或者子视图控制器来完成吗? - derpoliuk
3个回答

5
我通过使用队列来保留和重复使用一组实例,成功地获得了包含每个自己的子视图控制器的单元格集合的平滑滚动性能。GitHub列出了许多重用队列实现方案;经过对几乎所有方案进行了相当彻底的审查,我可以自信地推荐这个:https://github.com/acoomans/ACReuseQueue。根据ReadMe:重用队列是一种快速重用对象的方法,当对象分配和初始化耗时时特别有效。
你的视图未显示的原因是因为你没有在单元格子类中实例化子视图控制器,并且没有在cellForItemAtIndexPath中添加视图。这与作用域有关,你搞反了。
在单元格中添加子视图控制器的完美说明在这里提供:http://khanlou.com/2015/04/view-controllers-in-cells/

3
instantiateViewControllerWithIdentifier:的文档中可以了解到——每次调用此方法都会创建指定视图控制器的新实例。
你最好存储一组UIViewController,以便它们可以被重复使用,而不是存储ID并在每次调用tableView:cellForItemAtIndexPath:时实例化一个新的视图控制器。另外,由于UICollectionViewCell是可重用的,因此在每次调用tableView:cellForItemAtIndexPath:时不要将子视图添加到单元格中。相反,考虑创建自己的UICollectionViewCell子类,并具有mainView属性。您可以重写setMainView:以从其父视图中删除旧的mainView,然后设置新的frame(或添加布局约束)并将其作为子视图添加。另外,您应该实现适当的UIViewController containment方法。

我已经编辑了我的问题,你能看一下吗?我无法在数组中呈现控制器的视图。 - Gili Ariel
仅从您发布的片段很难判断。您的视图控制器不再从故事板实例化,这是有意为之吗? - johnpatrickmorgan

0

使用Swift进行更新(在集合视图的单元格中添加视图控制器)

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接