我有一个包含 1 个部分的 UICollectionView。用户可以从集合中删除单元格,我使用此代码进行删除:
[self.collectionView performBatchUpdates:^{
[self.collectionView deleteItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:i inSection:0]]];
[self.media removeObjectAtIndex:i];
} completion:nil];
除了集合中的最后一个单元格外,这对每个单元格都适用,每次都会使应用程序崩溃并出现错误:Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]:索引 3 超出范围 [0 .. 2]
NSZombies 没有向我显示堆栈跟踪,所以我在代码中访问数组的每一行都设置了一个断点,但没有命中,我发现这个错误是在 deleteItemsAtIndexPaths
之后抛出的, numberOfSectionsInCollectionView
和 sizeForItemAtIndexPath
,但在 numberOfItemsInSection
和 cellForItemAtIndexPath
之前
是什么导致了这次崩溃?我该如何调试它?
有一些类似的 SO 帖子,但是这个 2 年前的帖子没有答案 UICollectionView crash when deleting last item , 如果你想删除整个部分,这个只能解决问题:Removing the last Cell in UICollectionView makes a Crash
更新,这里是崩溃前运行的数据源委托(delegate)方法:
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(floorf(screenWidth/3), 200);
}
最佳答案
关于ios - 删除最后一个 UICollectionViewCell 时出现 NSRangeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36234269/