Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
UICollectionView';s performBatchUpdates崩溃iOS7_Ios_Objective C_Uicollectionview_Batch Updates - Fatal编程技术网

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
都有足够的对象。