UICollectionView';s performBatchUpdates崩溃iOS7
我注意到UICollectionView';s performBatchUpdates崩溃iOS7,ios,objective-c,uicollectionview,batch-updates,Ios,Objective C,Uicollectionview,Batch Updates,我注意到performBatchUpdates:completion:UICollectionView方法在iOS7(坏-崩溃)和iOS8(好)中的工作方式有一个奇怪的差异。以下是我使用的代码: [self.swapItems removeObject:self.swapItems[indexPath.row]]; [self.swapItemsGrid performBatchUpdates:^{ [self.swapItemsGrid deleteItemsAtIndexPaths
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中,performbatchUpdate:completion:
方法调用数据源方法collectionView:numberofitemsinssection:
,而在iOS7中则不调用,因此出现了一个错误,即不再尝试使用数据数组中对象的数据创建单元格
还有其他人遇到过这个问题吗?也许您想到了一个解决方案?
self.swapItemsGrid
可能在索引indexPath
处没有项。
要避免这种情况,请使用此选项检查是否存在数组中的indepath
出口:
-(BOOL)checkIndexPathExits:(NSIndexPath*)indexPath inArray:(NSArray*)array{
if (indexPath.section < array.count) {
if (indexPath.row < [array[indexPath.section] count]) {
return YES;
}
}
return NO;
}
-(BOOL)checkIndexPathExits:(nsindepath*)indepath inArray:(NSArray*)数组{
if(indexPath.section
希望这能有所帮助。哦,值得注意的是,只有当我删除一个项目(执行上面的代码-无崩溃)、添加一个项目、再次删除一个项目(执行上面的代码-崩溃)时,才会发生这种情况。在这两种情况下,
self.swapItems
数组是相同的。我遇到了同样的问题。你找到解决办法了吗?不幸的是,还没有。嗨。恐怕没那么容易。我只能在collectionView:cellForItemAtIndexPath:
方法中执行此类检查,如果该方法被调用,我必须返回UICollectionViewCell
的实例。这就是为什么有collectionView:numberofitemsinssection:
方法来确保数据数组对于每个请求的indepath
都有足够的对象。