UICollectionViewCell中的UiviewControllers

吉利·阿里尔(Gili Ariel)

我在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:-

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

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

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

同样,您应该实现适当的UIViewController包含的方法

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在UIPageViewController中重用的UIViewControllers

来自分类Dev

不同UIViewControllers中的UITableViews共享委托方法

来自分类Dev

在uicollectionviewcell中设置文本

来自分类Dev

UICollectionViewCell中的UILabel

来自分类Dev

UICollectionViewCell中的AVPlayerViewController错误?

来自分类Dev

在UICollectionViewCell中隐藏视图

来自分类Dev

UICollectionViewCell在iOS 8中消失

来自分类Dev

UIActivityIndicatorView在UICollectionViewCell中不旋转

来自分类Dev

确定UICollectionViewCell中的轻击元素

来自分类Dev

更改UICollectionViewCell中的标签位置

来自分类Dev

处理UICollectionViewCell中的scrollview大小

来自分类Dev

在UICollectionViewCell中添加/删除Observer

来自分类Dev

确定UICollectionViewCell中的轻击元素

来自分类Dev

在UicollectionViewCell中更新CollectionView的大小

来自分类Dev

iO中所有uiviewcontrollers中的操作栏

来自分类Dev

将UIViewControllers保留在内存中

来自分类Dev

在UITabBar的每个UINavigationController中重新加载所有UIViewControllers

来自分类Dev

获取iOS Swift中的UIViewControllers的所有列表

来自分类Dev

在appDelegate中访问情节提要和嵌套的UIViewControllers

来自分类Dev

将UIViewControllers保留在内存中

来自分类Dev

在自定义过渡中跨UIViewControllers“移动” UIViews?

来自分类Dev

UITableViewCell中的UICollectionViewCell中的UIImageView错误

来自分类Dev

如何在iOS 7中刷新UICollectionViewCell?

来自分类Dev

从单元格中删除UICollectionViewCell

来自分类Dev

UICollectionViewCell中的标签文本未更新

来自分类Dev

正确转换UICollectionViewCell中的子视图

来自分类Dev

在Swift中调整UICollectionViewCell大小时出错

来自分类Dev

UICollectionViewCell中的UITextField无法访问

来自分类Dev

UICollectionViewCell和UICollectionView中的Pan Gesture

Related 相关文章

  1. 1

    在UIPageViewController中重用的UIViewControllers

  2. 2

    不同UIViewControllers中的UITableViews共享委托方法

  3. 3

    在uicollectionviewcell中设置文本

  4. 4

    UICollectionViewCell中的UILabel

  5. 5

    UICollectionViewCell中的AVPlayerViewController错误?

  6. 6

    在UICollectionViewCell中隐藏视图

  7. 7

    UICollectionViewCell在iOS 8中消失

  8. 8

    UIActivityIndicatorView在UICollectionViewCell中不旋转

  9. 9

    确定UICollectionViewCell中的轻击元素

  10. 10

    更改UICollectionViewCell中的标签位置

  11. 11

    处理UICollectionViewCell中的scrollview大小

  12. 12

    在UICollectionViewCell中添加/删除Observer

  13. 13

    确定UICollectionViewCell中的轻击元素

  14. 14

    在UicollectionViewCell中更新CollectionView的大小

  15. 15

    iO中所有uiviewcontrollers中的操作栏

  16. 16

    将UIViewControllers保留在内存中

  17. 17

    在UITabBar的每个UINavigationController中重新加载所有UIViewControllers

  18. 18

    获取iOS Swift中的UIViewControllers的所有列表

  19. 19

    在appDelegate中访问情节提要和嵌套的UIViewControllers

  20. 20

    将UIViewControllers保留在内存中

  21. 21

    在自定义过渡中跨UIViewControllers“移动” UIViews?

  22. 22

    UITableViewCell中的UICollectionViewCell中的UIImageView错误

  23. 23

    如何在iOS 7中刷新UICollectionViewCell?

  24. 24

    从单元格中删除UICollectionViewCell

  25. 25

    UICollectionViewCell中的标签文本未更新

  26. 26

    正确转换UICollectionViewCell中的子视图

  27. 27

    在Swift中调整UICollectionViewCell大小时出错

  28. 28

    UICollectionViewCell中的UITextField无法访问

  29. 29

    UICollectionViewCell和UICollectionView中的Pan Gesture

热门标签

归档