dequeueReusableCellWithIdentifier不重用单元格 [英] dequeueReusableCellWithIdentifier not reusing cells

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

问题描述

我有一个 UICollectionView ,带有自定义单元子类 UICollectionViewCell 。在代码中我完成了以下操作:

I have a UICollectionView with a custom cell subclassing UICollectionViewCell. And in the code I have done the following:

 [self.collectionView_ registerClass:[AHPinterestCell class]
        forCellWithReuseIdentifier:@"AHPinterestCell"];

这就是我的 cellForItem

  AHPinterestCell *cell =
                (AHPinterestCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"AHPinterestCell"
                                                                             forIndexPath:indexPath];

然而,它似乎没有重复使用该单元格。在我的每个屏幕的集合视图中它显示大约9-10个单元格,但是当我进行无限滚动然后我调用 insertItemsAtIndexPath 它调用 initWithFrame 我的自定义单元格上的方法,虽然它应该重用我已经拥有的单元格。这是为什么?

however, it seems that it's not reusing the cell. In my collection view per screen it is showing about 9-10 cells, however when I do infinite scroll and then I call insertItemsAtIndexPath it calls the initWithFrame method on my custom cell, while it should probably reuse the cells I already have. Why is this?

编辑:

我正在添加示例演示项目这说明了问题,可以在这里找到xcode项目的链接。当你到达底部时,它基本上做了一个无限滚动,它只是添加了更多东西。但是当你这样做时,它会再次调用init方法。

I am adding a sample demo project that illustrates the problem, the link to the xcode project can be found here. It's essentially doing an infinite scroll when you reach to the bottom, it just appends more stuff into it. But when you do so it's going to call the init method again.

推荐答案

简而言之,你的代码运行正常。正如预期的那样,当单元格从屏幕滚动时,它们最终会被标记为可以重复使用。当你调用 dequeueReusableCellWithReuseIdentifier 时,如果有一个可以重用的单元格,它就会这样做。如果没有,它会创建一个。

In short, your code works fine. As expected, as cells scroll off the screen, they are eventually flagged as being available for reuse. And when you call dequeueReusableCellWithReuseIdentifier, if there is a cell available for reuse, it will do so. If not, it creates one.

如果您快速或连续滚动,您将看到创建大量单元格的时间。但是如果你做一些简短的卷轴,放手,暂停,让UI赶上并重复,然后你会看到很少有细胞被创建。

The time that you'll see a lot of cells being created is if you scroll fast or continually. But if you do short little scrolls, let go, pause, let the UI catch up and repeat, then you'll see very few cells being created.

我认为这意味着iOS优先考虑UI并在旧单元格的出列上创建新单元格,从而允许它们重用。因此,如果你快速翻转,它就很难赶上并将旧单元标记为可重用。这可能并不完全是坏事,因为这可能是集合视图如此顺畅的流程。标记旧单元格以供重用是iOS在此处不太重要的事情之一。但很明显,如果内存紧张,这可能是一个问题。

I interpret this as meaning that iOS prioritizes the UI and the creation of new cells over the dequeuing of old cells, allowing their reuse. So if you flip quickly, it has trouble catching up and flagging old cells as available for reuse. This is probably not altogether bad, as this is probably what gives collection views such smooth flow. Flagging old cells for reuse is one of the less important things for iOS to do here. But clearly if memory is tight, this might be a problem.

顺便说一句,如果你放一个 NSLog dealloc 中,您也会注意到,当在完全快速滚动浏览集合视图后UI最终赶上时,它显然有一些逻辑说gee,我有更多的备用电池而不是我真正需要的电池,我将摆脱其中的一些。实际上,这是一个非常聪明的实现。关注速度,但一旦UI安静下来就会进行一些内存优化。

By the way, if you put a NSLog in dealloc, too, you'll notice that when the UI finally catches up after doing a really fast scroll through the collection view, it clearly has some logic that says "gee, I've got more spare cells than I really need, I'm going to get rid of some of these." It's a pretty clever implementation, actually. A focus on speed, but with some memory optimizations that take place once the UI quiets down.

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

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