我注意到 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/