【问题标题】:UiviewControllers inside UICollectionViewCellUICollectionViewCell 内的 UiviewControllers
【发布时间】: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


【解决方案1】:

使用 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

【讨论】:

    【解决方案2】:

    通过使用队列来保留和重用一定数量的实例,我能够获得包含每个自己的子视图控制器的单元格集合的平滑滚动性能。

    GitHub 列出了一些重用队列的实现;而且,在对几乎每一个都进行了相当彻底的审查之后,我可以自信地推荐这个:

    https://github.com/acoomans/ACReuseQueue

    根据自述文件: 重用队列是一种在对象分配和初始化耗时时快速重用对象的方法。

    您的视图未显示的原因是您没有从单元子类中实例化子视图控制器,也没有从 cellForItemAtIndexPath 中添加视图。这是关于范围的,而你已经把它倒退了。

    此处提供了将子视图控制器添加到单元格的完美说明:

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

    【讨论】:

      【解决方案3】:

      来自instantiateViewControllerWithIdentifier:的文档-

      此方法每次调用时都会创建指定视图控制器的新实例

      您最好存储UIViewControllers 的数组以便它们可以重复使用,而不是存储 ID 并在每次调用 tableView:cellForItemAtIndexPath: 时实例化一个新的视图控制器。

      另外,由于UICollectionViewCells 被重复使用,所以每次调用tableView:cellForItemAtIndexPath: 时都向单元格添加子视图是不明智的。相反,请考虑使用 mainView 属性创建您自己的 UICollectionViewCell 子类。在设置新的框架(或添加布局约束)并将其添加为子视图之前,您可以覆盖 setMainView: 以从其父视图中删除旧的 mainView。

      你也应该实现正确的UIViewController containment的方法。

      【讨论】:

      • 我已经编辑了我的问题,你可以看看吗?我无法在数组中显示控制器的视图。
      • 仅从您发布的 sn-p 很难判断。您的视图控制器不再从情节提要中实例化,这是故意的吗?
      猜你喜欢
      • 2019-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多