UICollectionView 不重用单元格

Posted

技术标签:

【中文标题】UICollectionView 不重用单元格【英文标题】:UICollectionView do not reuse cells 【发布时间】:2013-10-17 01:50:04 【问题描述】:

我在 ios 7 上重用单元格和 UICollectionView 时遇到问题。我的代码在 iOS 6 上运行良好,但在 iOS 7 中,调用 dequeueReusableCellWithReuseIdentifier 后它不会重用单元格(不要调用 prepareForReuse)。

即使此代码https://developer.apple.com/library/ios/samplecode/CollectionView-Simple/Introduction/Intro.html#//apple_ref/doc/uid/DTS40012860 不能在 iOS7 上重用单元格(但在 iOS6 上运行良好),在每个 dequeueReusableCellWithReuseIdentifier 上它都会创建一个新单元格并释放旧单元格。是否有一些新的东西阻止细胞被重复使用?

我的细胞足够大,不重复使用它们是非常低效的。我注意到在 iOS 7 上存在延迟,但在 iOS 6 上没有,因为它们没有在 iOS 7 中被重用。

【问题讨论】:

我也有同样的问题!以为这是我的设置或其他原因,但我刚刚测试了苹果示例代码并且得到了相同的结果! 你得了+1,我得了-1。帮我解决同样的问题 @in.disee 您能否将我最近的答案设置为正确的答案,以便更加明显,因为它确实是正确的答案?谢谢 【参考方案1】:

更新 2: 事实证明,这个答案是不正确的。 请参阅下面的答案或点击此链接https://***.com/a/20147799/814389。


更新

所以我重新审视了这个答案,因为我发现了更多关于这个错误..

我拿起了所有可用的设备,对每台设备进行了相同的测试,结果如下:

DEVICE          OS Version      CELL REUSE
=============   =============   =============
iPad 4          7.0.0           YES
iPad 4          7.0.3           YES
iPad 3          7.0.3           NO
iPad 2          7.0.3           NO
iPad Mini       7.0.0           YES
iPad Mini       7.0.3           YES
iPhone 5s       7.0.3           YES
iPhone 4        7.0.2           YES
iPhone 4        7.0.3           YES

如您所见,由于某种原因,单元重用似乎不适用于旧 iPad(无法渲染模糊的 iPad)。

我最初认为,由于某种性能问题,Apple 可能刚刚阻止在旧 iPad 上重复使用,但如果这是有道理的,iPhone 4 也会显示相同的结果。

为了在我的应用程序中解决这个问题,我在我的 collectionViewController 中有一个 NSMutableDictionary,我将我的单元格存储在其中,键是 indexPath。在我的情况下,这没关系,因为我只有大约 9 个单元格及其indexPaths 永远不会改变,但如果您需要更灵活的东西,那么检查 PSTCollectionView (https://github.com/steipete/PSTCollectionView) 可能是个好主意


刚刚在物理设备上进行了测试。它似乎在 iOS 7 和 6 上都可以正常工作,但在 iOS 7 模拟器上却不行!!!

只需在 collectionView 示例中添加一些日志即可:

@implementation Cell

- (id)initWithCoder:(NSCoder *)aDecoder

    NSLog(@"%s",__func__);
    self = [super initWithCoder:aDecoder];
    if (self)
    
        CustomCellBackground *backgroundView = [[CustomCellBackground alloc] initWithFrame:CGRectZero];
        self.selectedBackgroundView = backgroundView;
    
    return self;


-(void)prepareForReuse

    NSLog(@"%s",__func__);


-(void)dealloc

    NSLog(@"%s",__func__);


@end

然后在所有三个设备上滚动到底部,这是输出:

iOS 7 模拟器

2013-10-09 17:42:45.798 CollViewSmpl[9547:a0b] -[Cell initWithCoder:]
2013-10-09 17:42:45.807 CollViewSmpl[9547:a0b] -[Cell initWithCoder:]
2013-10-09 17:42:45.811 CollViewSmpl[9547:a0b] -[Cell initWithCoder:]
2013-10-09 17:42:45.841 CollViewSmpl[9547:a0b] -[Cell initWithCoder:]
2013-10-09 17:42:45.844 CollViewSmpl[9547:a0b] -[Cell initWithCoder:]
2013-10-09 17:42:45.848 CollViewSmpl[9547:a0b] -[Cell initWithCoder:]
2013-10-09 17:42:45.852 CollViewSmpl[9547:a0b] -[Cell initWithCoder:]
2013-10-09 17:42:45.857 CollViewSmpl[9547:a0b] -[Cell initWithCoder:]
2013-10-09 17:42:47.080 CollViewSmpl[9547:a0b] -[Cell initWithCoder:]
2013-10-09 17:42:47.083 CollViewSmpl[9547:a0b] -[Cell initWithCoder:]
2013-10-09 17:42:47.181 CollViewSmpl[9547:a0b] -[Cell initWithCoder:]
2013-10-09 17:42:47.183 CollViewSmpl[9547:a0b] -[Cell initWithCoder:]
2013-10-09 17:42:47.208 CollViewSmpl[9547:a0b] -[Cell dealloc]
2013-10-09 17:42:47.208 CollViewSmpl[9547:a0b] -[Cell dealloc]
2013-10-09 17:42:47.214 CollViewSmpl[9547:a0b] -[Cell initWithCoder:]
2013-10-09 17:42:47.218 CollViewSmpl[9547:a0b] -[Cell initWithCoder:]
2013-10-09 17:42:47.245 CollViewSmpl[9547:a0b] -[Cell dealloc]
2013-10-09 17:42:47.246 CollViewSmpl[9547:a0b] -[Cell dealloc]
2013-10-09 17:42:47.264 CollViewSmpl[9547:a0b] -[Cell initWithCoder:]
2013-10-09 17:42:47.268 CollViewSmpl[9547:a0b] -[Cell initWithCoder:]
2013-10-09 17:42:47.289 CollViewSmpl[9547:a0b] -[Cell dealloc]
2013-10-09 17:42:47.290 CollViewSmpl[9547:a0b] -[Cell dealloc]
2013-10-09 17:42:47.317 CollViewSmpl[9547:a0b] -[Cell initWithCoder:]
2013-10-09 17:42:47.322 CollViewSmpl[9547:a0b] -[Cell initWithCoder:]
2013-10-09 17:42:47.343 CollViewSmpl[9547:a0b] -[Cell dealloc]
2013-10-09 17:42:47.344 CollViewSmpl[9547:a0b] -[Cell dealloc]
2013-10-09 17:42:47.364 CollViewSmpl[9547:a0b] -[Cell initWithCoder:]
2013-10-09 17:42:47.367 CollViewSmpl[9547:a0b] -[Cell initWithCoder:]
2013-10-09 17:42:47.401 CollViewSmpl[9547:a0b] -[Cell dealloc]
2013-10-09 17:42:47.402 CollViewSmpl[9547:a0b] -[Cell dealloc]
2013-10-09 17:42:47.430 CollViewSmpl[9547:a0b] -[Cell initWithCoder:]
2013-10-09 17:42:47.432 CollViewSmpl[9547:a0b] -[Cell initWithCoder:]
2013-10-09 17:42:47.472 CollViewSmpl[9547:a0b] -[Cell dealloc]
2013-10-09 17:42:47.472 CollViewSmpl[9547:a0b] -[Cell dealloc]
2013-10-09 17:42:47.498 CollViewSmpl[9547:a0b] -[Cell initWithCoder:]
2013-10-09 17:42:47.505 CollViewSmpl[9547:a0b] -[Cell initWithCoder:]
2013-10-09 17:42:47.561 CollViewSmpl[9547:a0b] -[Cell dealloc]
2013-10-09 17:42:47.562 CollViewSmpl[9547:a0b] -[Cell dealloc]
2013-10-09 17:42:47.585 CollViewSmpl[9547:a0b] -[Cell initWithCoder:]
2013-10-09 17:42:47.587 CollViewSmpl[9547:a0b] -[Cell initWithCoder:]
2013-10-09 17:42:47.624 CollViewSmpl[9547:a0b] -[Cell dealloc]
2013-10-09 17:42:47.624 CollViewSmpl[9547:a0b] -[Cell dealloc]
2013-10-09 17:42:47.669 CollViewSmpl[9547:a0b] -[Cell initWithCoder:]
2013-10-09 17:42:47.674 CollViewSmpl[9547:a0b] -[Cell initWithCoder:]
2013-10-09 17:42:47.797 CollViewSmpl[9547:a0b] -[Cell initWithCoder:]
2013-10-09 17:42:47.799 CollViewSmpl[9547:a0b] -[Cell initWithCoder:]
2013-10-09 17:42:47.809 CollViewSmpl[9547:a0b] -[Cell dealloc]
2013-10-09 17:42:47.809 CollViewSmpl[9547:a0b] -[Cell dealloc]
2013-10-09 17:42:47.810 CollViewSmpl[9547:a0b] -[Cell dealloc]
2013-10-09 17:42:47.810 CollViewSmpl[9547:a0b] -[Cell dealloc]
2013-10-09 17:42:47.964 CollViewSmpl[9547:a0b] -[Cell initWithCoder:]
2013-10-09 17:42:47.966 CollViewSmpl[9547:a0b] -[Cell initWithCoder:]
2013-10-09 17:42:47.987 CollViewSmpl[9547:a0b] -[Cell dealloc]
2013-10-09 17:42:47.987 CollViewSmpl[9547:a0b] -[Cell dealloc]

iOS 6 设备 (iPhone 5)

2013-10-09 17:45:42.173 CollViewSmpl[187:907] -[Cell initWithCoder:]
2013-10-09 17:45:42.191 CollViewSmpl[187:907] -[Cell initWithCoder:]
2013-10-09 17:45:42.205 CollViewSmpl[187:907] -[Cell initWithCoder:]
2013-10-09 17:45:42.217 CollViewSmpl[187:907] -[Cell initWithCoder:]
2013-10-09 17:45:42.230 CollViewSmpl[187:907] -[Cell initWithCoder:]
2013-10-09 17:45:42.242 CollViewSmpl[187:907] -[Cell initWithCoder:]
2013-10-09 17:45:42.253 CollViewSmpl[187:907] -[Cell initWithCoder:]
2013-10-09 17:45:42.264 CollViewSmpl[187:907] -[Cell initWithCoder:]
2013-10-09 17:45:43.630 CollViewSmpl[187:907] -[Cell initWithCoder:]
2013-10-09 17:45:43.640 CollViewSmpl[187:907] -[Cell initWithCoder:]
2013-10-09 17:45:43.697 CollViewSmpl[187:907] -[Cell prepareForReuse]
2013-10-09 17:45:43.706 CollViewSmpl[187:907] -[Cell prepareForReuse]
2013-10-09 17:45:43.777 CollViewSmpl[187:907] -[Cell prepareForReuse]
2013-10-09 17:45:43.791 CollViewSmpl[187:907] -[Cell prepareForReuse]
2013-10-09 17:45:43.844 CollViewSmpl[187:907] -[Cell prepareForReuse]
2013-10-09 17:45:43.855 CollViewSmpl[187:907] -[Cell prepareForReuse]
2013-10-09 17:45:43.927 CollViewSmpl[187:907] -[Cell prepareForReuse]
2013-10-09 17:45:43.937 CollViewSmpl[187:907] -[Cell prepareForReuse]
2013-10-09 17:45:44.027 CollViewSmpl[187:907] -[Cell prepareForReuse]
2013-10-09 17:45:44.037 CollViewSmpl[187:907] -[Cell prepareForReuse]
2013-10-09 17:45:44.144 CollViewSmpl[187:907] -[Cell prepareForReuse]
2013-10-09 17:45:44.155 CollViewSmpl[187:907] -[Cell prepareForReuse]
2013-10-09 17:45:44.311 CollViewSmpl[187:907] -[Cell prepareForReuse]
2013-10-09 17:45:44.324 CollViewSmpl[187:907] -[Cell prepareForReuse]
2013-10-09 17:45:44.560 CollViewSmpl[187:907] -[Cell prepareForReuse]
2013-10-09 17:45:44.571 CollViewSmpl[187:907] -[Cell prepareForReuse]
2013-10-09 17:45:45.027 CollViewSmpl[187:907] -[Cell prepareForReuse]
2013-10-09 17:45:45.040 CollViewSmpl[187:907] -[Cell prepareForReuse]
2013-10-09 17:45:45.397 CollViewSmpl[187:907] -[Cell prepareForReuse]
2013-10-09 17:45:45.407 CollViewSmpl[187:907] -[Cell prepareForReuse]
2013-10-09 17:45:45.494 CollViewSmpl[187:907] -[Cell prepareForReuse]
2013-10-09 17:45:45.503 CollViewSmpl[187:907] -[Cell prepareForReuse]

iOS 7 设备 (iPhone 5s)

2013-10-09 17:44:37.603 CollViewSmpl[871:60b] -[Cell initWithCoder:]
2013-10-09 17:44:38.015 CollViewSmpl[871:60b] -[Cell initWithCoder:]
2013-10-09 17:44:38.029 CollViewSmpl[871:60b] -[Cell initWithCoder:]
2013-10-09 17:44:38.037 CollViewSmpl[871:60b] -[Cell initWithCoder:]
2013-10-09 17:44:38.045 CollViewSmpl[871:60b] -[Cell initWithCoder:]
2013-10-09 17:44:38.053 CollViewSmpl[871:60b] -[Cell initWithCoder:]
2013-10-09 17:44:38.061 CollViewSmpl[871:60b] -[Cell initWithCoder:]
2013-10-09 17:44:38.071 CollViewSmpl[871:60b] -[Cell initWithCoder:]
2013-10-09 17:44:39.470 CollViewSmpl[871:60b] -[Cell initWithCoder:]
2013-10-09 17:44:39.483 CollViewSmpl[871:60b] -[Cell initWithCoder:]
2013-10-09 17:44:39.535 CollViewSmpl[871:60b] -[Cell prepareForReuse]
2013-10-09 17:44:39.540 CollViewSmpl[871:60b] -[Cell prepareForReuse]
2013-10-09 17:44:39.583 CollViewSmpl[871:60b] -[Cell prepareForReuse]
2013-10-09 17:44:39.587 CollViewSmpl[871:60b] -[Cell prepareForReuse]
2013-10-09 17:44:39.633 CollViewSmpl[871:60b] -[Cell prepareForReuse]
2013-10-09 17:44:39.637 CollViewSmpl[871:60b] -[Cell prepareForReuse]
2013-10-09 17:44:39.683 CollViewSmpl[871:60b] -[Cell prepareForReuse]
2013-10-09 17:44:39.688 CollViewSmpl[871:60b] -[Cell prepareForReuse]
2013-10-09 17:44:39.733 CollViewSmpl[871:60b] -[Cell prepareForReuse]
2013-10-09 17:44:39.737 CollViewSmpl[871:60b] -[Cell prepareForReuse]
2013-10-09 17:44:39.783 CollViewSmpl[871:60b] -[Cell initWithCoder:]
2013-10-09 17:44:39.791 CollViewSmpl[871:60b] -[Cell initWithCoder:]
2013-10-09 17:44:39.866 CollViewSmpl[871:60b] -[Cell prepareForReuse]
2013-10-09 17:44:39.870 CollViewSmpl[871:60b] -[Cell prepareForReuse]
2013-10-09 17:44:39.933 CollViewSmpl[871:60b] -[Cell prepareForReuse]
2013-10-09 17:44:39.938 CollViewSmpl[871:60b] -[Cell prepareForReuse]
2013-10-09 17:44:40.033 CollViewSmpl[871:60b] -[Cell prepareForReuse]
2013-10-09 17:44:40.036 CollViewSmpl[871:60b] -[Cell prepareForReuse]
2013-10-09 17:44:40.149 CollViewSmpl[871:60b] -[Cell prepareForReuse]
2013-10-09 17:44:40.152 CollViewSmpl[871:60b] -[Cell prepareForReuse]
2013-10-09 17:44:40.300 CollViewSmpl[871:60b] -[Cell prepareForReuse]
2013-10-09 17:44:40.304 CollViewSmpl[871:60b] -[Cell prepareForReuse]
2013-10-09 17:44:40.650 CollViewSmpl[871:60b] -[Cell prepareForReuse]
2013-10-09 17:44:40.652 CollViewSmpl[871:60b] -[Cell prepareForReuse]

您可以看出在 iOS 6 和 7 中重用之间发生了一些变化,因为 1)它在模拟器中不起作用,以及 2)如果您一开始就进行非常快速的滚动,则单元格最初不是准备好重复使用,所以它必须创建一个新的来弥补 iOS 6 没有的地方(参见我的日志)。

我花了半天时间试图修复一个只发生在模拟器上的错误。

【讨论】:

谢谢,伙计。我的一半时间也浪费了,做和你一样的伐木。我们周围没有可以测试的设备 =) 我遇到了相反的问题:它在 iPad 模拟器中运行良好,但无法在运行 iOS 7.0.3 的第三代 iPad 上重复使用单元格。 你用苹果示例代码测试过还是你自己的代码?我还没有机会亲自检查 7.0.3 我们在 iPhone 5s 7.0.3 上的自定义应用程序中没有看到任何单元格重复使用。 -prepareForReuse 永远不会被调用,仪器显示 25% 的时间用于单元笔尖加载。 大家好,我刚刚遇到了同样的问题,但我的测试比你们的测试更奇怪,因为使用了三个不同的 iPad 3,只有一个没有重复使用单元,而另外两个是。这与一些可访问性设置有关,请参阅下面的答案以进行检查;)【参考方案2】:

我今天在我的 iPad 3 上遇到了同样的问题(我测试的 iPad 3 中只有三分之一),我发现它与全局辅助功能设置有关。解决方案是仔细检查无障碍面板中的每个选项都已禁用。 我认为某些选项(例如更大的字体)可以保持启用状态,但我还没有详细检查哪一个。

说明

查看堆栈跟踪可以看到:

#0  0x003121ce in -[MyCollectionViewCell initWithFrame:]
#1  0x32f945ec in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:]
#2  0x04466656 in -[UICollectionViewAccessibility(SafeCategory) _dequeueReusableViewOfKind:withIdentifier:forIndexPath:]
#3  0x32f9414a in -[UICollectionView dequeueReusableCellWithReuseIdentifier:forIndexPath:]
#4  0x001767cc in -[MyCollectionView collectionView:cellForItemAtIndexPath:]

如您所见,有一个对-[UICollectionViewAccessibility(SafeCategory) _dequeueReusableViewOfKind:withIdentifier:forIndexPath:] 的调用,它引用了一些关于辅助功能的内容。于是我去了设置应用中的辅助功能设置,我发现我的辅助功能快捷方式设置是“开关控制”而不是什么都没有。所以我禁用了它,我再次运行应用程序,我的堆栈跟踪现在很好:

#0  0x00313658 in -[MyCollectionViewCell prepareForReuse]
#1  0x32f942e6 in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:]
#2  0x32f9414a in -[UICollectionView dequeueReusableCellWithReuseIdentifier:forIndexPath:]
#3  0x001767cc in -[MyCollectionView collectionView:cellForItemAtIndexPath:]

【讨论】:

好发现!你在 iPad 4 上测试过吗?我有一段时间没有靠近任何设备,所以无法亲自尝试 不,抱歉,我也没有 我刚刚在 iPad Mini 上进行了测试,同样的事情发生了!事实证明,我测试的 iPad 3 也启用了快捷方式!好发现!如果你得到时间 rdar://15357491,还有一个雷达可以复制 非常有趣,感谢您跟踪此内容。我已经确认取消选中辅助功能快捷方式下的所有选项可以修复我的 iPad 3 上的 prepareForReuse 问题。多么随机... 天哪。我很高兴我偶然发现了这一点。为什么它没有重复使用我的细胞,这让我发疯了。非常感谢您的发布。绝对是我的捷径。【参考方案3】:

这是 iOS 7 中的一个已知问题:http://openradar.appspot.com/15357491

【讨论】:

以上是关于UICollectionView 不重用单元格的主要内容,如果未能解决你的问题,请参考以下文章

如何不在 UICollectionView 中重用单元格 - Swift

重用单元格 UICollectionView - 某些单元格没有被选中

UICollectionView 重用单元格问题

拖放 UICollectionView 单元格重用问题

在 UICollectionView 上禁用单元格重用

UICollectionView 重用单元格背景问题