【问题标题】:NSRangeException when deleting last UICollectionViewCell删除最后一个 UICollectionViewCell 时出现 NSRangeException
【发布时间】: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 没有向我显示堆栈跟踪,因此我在代码中访问数组但没有命中的每一行上都设置了一个断点,我发现在 deleteItemsAtIndexPathsnumberOfSectionsInCollectionView 和 @ 之后会引发此错误987654327@,但在numberOfItemsInSectioncellForItemAtIndexPath 之前

什么可能导致此崩溃?如何调试?

有一些类似的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]
  • 在删除单元格并从数组中移除对象后尝试重新加载集合视图。
  • 我已经尝试了deleteItemsAtIndexPathsremoveObjectAtIndex 的所有可能顺序,无论有无performBatchUpdates blcok,它们都以相同的方式崩溃
  • 您能否编辑您的问题以包括您对集合视图数据源和委托方法的实现。

标签: ios objective-c uicollectionview uicollectionviewcell


【解决方案1】:

只需放置异常断点,您就会发现它到底在哪里崩溃了。

【讨论】:

  • 我已经编辑了我的答案以包括有助于跟踪错误的内容。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-05
相关资源
最近更新 更多