无需重用即可加载 UICollectionView 单元格 [英] Load UICollectionView cells without reuse

查看:32
本文介绍了无需重用即可加载 UICollectionView 单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个只显示三个单元格的 collectionView.每个单元格都属于我在故事板中设置的相同类型的自定义 collectionViewCell.我只希望每个单元仅实例化一次;未重复使用.

I have a collectionView that will only display three cells. Each cell is of the same type of custom collectionViewCell that I've set up in storyboard. I only want each cell instantiated once and only once; not reused.

如何从 cellForItemAtIndexPath: 未重用的数据源方法返回单元格?可以这样做吗?

How can I return cells from cellForItemAtIndexPath: datasource method that aren't reused? Is it possible to do this?

推荐答案

重用 UICollectionViewCell 并不意味着你要摆脱以前不再出现在屏幕上的那些.H1>

这是一个提高内存效率的结构化过程,其工作原理如下:

Reusing a UICollectionViewCell doesn't mean you're getting rid of the previous ones that are no longer on the screen.

It's a structured process for memory efficiency and here's how it works:

  1. UICollectionView 根据您需要的 UICollectionViewLayout 自动生成不同数量的单元格.
  2. 这些单元格建立在 UIScrollView 之上,重用它们,而不是为最终出现的每个单元格分配内存.
  3. 想象数据集是一个轮子";它的大小取决于您需要创建的单元格数量.
  4. 让我们说你的轮子"共有 100 个不同的视图";这将填充单元格
  5. 如果屏幕一次只能显示 5 个,那么加载所有 100 个是没有意义的......只是将在视图中的 5 和那些准备进入滚动时的屏幕..
  6. 通过使用 UICollectionViewDataSource 协议,您的集合视图会重新填充刚刚离开屏幕的单元格,并将其放在要重用"的显示列表的下方或上方.而不是导致内存问题.
  1. There are a distinct number of cells automatically generated by the UICollectionView based on the UICollectionViewLayout you're needing.
  2. These cells are built upon a UIScrollView that reuses them instead of allocating memory for every cell that will eventually be appearing.
  3. imagine the dataset being a "wheel" and it's size dependent on how many cells you're needing to create.
  4. Let's say your "wheel" has a total of 100 different "views" that will populate the cells
  5. If the screen can only show 5 at a time, it wouldn't make sense to load all 100... Just the 5 that will be within the view and the ones preparing to enter the screen as you're scrolling..
  6. By Using the UICollectionViewDataSource Protocols, your collection view repopulates the cell that just left the screen and puts it below, or above, the displayed list to be "reused" instead of causing memory problems.

Collection Views 非常高效,我建议使用它们内置的内存节省 dequeuereusablecellwithidentifier.

Collection Views are incredibly efficient and I would recommend utilizing their built in Memory saving dequeuereusablecellwithidentifier.

通过将所需的必要数据放入 NSArrayNSArray代码>.

You would get the result you're wanting by putting the Necessary Data you'll need within the cells (possibly making a subclass of the UICollectionViewCell if needed) inside a NSArray.

您将使用 cellForItemAtIndexPath 访问每个对象(在我们的示例中为 1 - 100),并且在滚动时,重用单元格的基本原则将显示需要显示的 NSArray 对象.

You'll access each object (1 - 100 in our example) using the cellForItemAtIndexPath and as your scroll, the basic principle of reusing the cells will display the NSArray objects that are needed to be displayed.

我希望这是有道理的,它可以帮助您解决问题!

I'm hoping this makes sense and it helps you solve your problem!

这篇关于无需重用即可加载 UICollectionView 单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆