ios - CollectionView performBatchUpdates 崩溃 iOS 7

标签 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 中则没有,因此不再尝试使用数据数组中的对象的数据创建单元格的错误。

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

最佳答案

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;
}

希望这会有所帮助。

关于ios - CollectionView performBatchUpdates 崩溃 iOS 7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31535698/

相关文章:

ios - 在哪里保存 iOS 应用程序中的数据库?

ios - 购买应用程序后,应用程序在启动时崩溃。产品标识符=无?

iOS:是否可以将注释放在 map 的中心并在注释始终保持在中心的情况下移动 map ?

具有动态高度的 UICollectionViewLayout - 但不使用流布局

ios - UICollectionView 中 80% 屏幕和 20% 屏幕的计算

ios - 将 IndexPath 行值传递给委托(delegate)函数而不是 Sender.tag 以删除图像项目 CollectionView Swift

ios - 如何使用 NSRange 从字符串中提取特定句子?

iOS:使用应用委托(delegate)来存放全局变量

objective-c - 尝试使用 NSUserDefaults 保存和设置 UISwitch 状态

iphone - 以编程方式更改基于 UIWebkit 的应用程序的方向