UICollectionView的performBatchUpdates崩溃iOS7

时间:2015-07-21 09:49:26

标签: ios objective-c uicollectionview batch-updates

我注意到performBatchUpdates:completion: UICollectionView方法在iOS7(糟糕崩溃)和iOS8(良好)中的工作方式存在奇怪的差异。这是我使用的代码:

[self.swapItems removeObject:self.swapItems[indexPath.row]];

[self.swapItemsGrid performBatchUpdates:^{
    [self.swapItemsGrid deleteItemsAtIndexPaths:@[indexPath]];
} completion:^(BOOL finished) {
    [self layoutViews];
}];

在iOS8中它可以正常工作,而在iOS7中崩溃时出现以下错误:

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'

一点调试表明,在iOS8中,performBatchUpdates:completion:方法调用数据源方法collectionView:numberOfItemsInSection:,而在iOS7中则不调用,因此尝试使用不再有数据数组中的对象数据。

还有其他人遇到过这个问题吗?也许你有一个解决方案吗?

1 个答案:

答案 0 :(得分:0)

self.swapItemsGrid可能没有索引indexPath的项目。 为避免这种情况,请使用此选项检查数组中的indexPath出口:

-(BOOL)checkIndexPathExits:(NSIndexPath*)indexPath inArray:(NSArray*)array{
    if (indexPath.section < array.count) {
        if (indexPath.row < [array[indexPath.section] count]) {
            return YES;
        }
    }
    return NO;
}

希望这可以提供帮助。