cocoa-touch - UICollectionView不重用单元格

标签 cocoa-touch ios7 uicollectionview reusability uicollectionviewcell

我在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中没有重复使用它们。

最佳答案

更新2:
原来,这个答案是不正确的。
请查看下面的答案,或单击此链接https://stackoverflow.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)上运行。

我最初以为是由于某种性能问题,苹果可能刚刚阻止了在旧iPad上的重用,但是如果这是合理的话,iPhone 4也会显示出相同的结果。

为了在我的应用程序中解决这个问题,我在collectionViewController中有一个NSMutableDictionary,并将其中的单元格存储在其中,键为indexPath。在我的情况下,这是可以的,因为我只有大约9个单元格,而它们的indexPath从未更改但是,如果您需要更灵活的东西,那么结帐PSTCollectionView(https://github.com/steipete/PSTCollectionView)也许是个好主意。



刚刚在物理设备上对此进行了测试..它似乎在iOS 7和6上都可以正常工作,但在iOS 7 Simulator上却不能工作!

只需将一些日志放入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没有的地方(请参阅我的日志)。

我花了一半的时间尝试修复仅在模拟器上发生的错误。

关于cocoa-touch - UICollectionView不重用单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19276509/

相关文章:

html - iOS7 Safari 和 Chrome 中奇怪的 CSS 拉伸(stretch)问题

swift - 用户与数据库驱动的 UI 更新

ios - UICollectionView - 在哪里设计每个单元格。给定的帧不能正常工作

ios - 隐藏工具栏并增加主屏幕 View 高度

objective-c - 从 UIView 的 subview 获取 UINavController?

iOS - UITextView + NSUndoManager

ios - 如何重置可重复使用细胞的外观?

iphone - 如何将对象转发到单独的 subview ?

ios - 在 iOS 7 中禁用打印屏幕

ios - 滚动时获取 UICollectionView 的第一个单元格/项目 - iOS