【发布时间】:2016-03-26 10:33:32
【问题描述】:
我有一个带有 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:]: index 3 beyond bounds [0 .. 2]
NSZombies 没有向我显示堆栈跟踪,因此我在代码中访问数组但没有命中的每一行上都设置了一个断点,我发现在 deleteItemsAtIndexPaths、numberOfSectionsInCollectionView 和 @ 之后会引发此错误987654327@,但在numberOfItemsInSection 和cellForItemAtIndexPath 之前
什么可能导致此崩溃?如何调试?
有一些类似的SO帖子,但是2年前的这个没有答案UICollectionView crash when deleting last item,而这个只有你想删除整个部分才能解决问题:Removing the last Cell in UICollectionView makes a Crash
更新,这里是崩溃前运行的数据源委托方法:
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(floorf(screenWidth/3), 200);
}
【问题讨论】:
-
用户点击的任何单元格索引。它不会在这些线上崩溃
-
在使用批量更新块之前尝试使用 [self.media removeObjectAtIndex:i]
-
在删除单元格并从数组中移除对象后尝试重新加载集合视图。
-
我已经尝试了
deleteItemsAtIndexPaths、removeObjectAtIndex的所有可能顺序,无论有无performBatchUpdatesblcok,它们都以相同的方式崩溃 -
您能否编辑您的问题以包括您对集合视图数据源和委托方法的实现。
标签: ios objective-c uicollectionview uicollectionviewcell