删除最后一项时UICollectionView崩溃

每当我删除一个项目时,我都会尝试更新uicollection视图。 所有单元格都删除正常,但如果我删除该集合视图中的最后一个单元格应用程序崩溃,我放了

[self.collectionview performBatchUpdates:^{ [postarray removeObjectAtIndex:indexPath.item]; [self.collectionview deleteItemsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexPath.row inSection:1]]]; } completion:^(BOOL finished) {}]; 

我得到的错误是’NSRangeException’,原因:’ * – [__ NSArrayI objectAtIndex:]:索引17超出边界[0 .. 16]’。 甚至数组和集合视图项都以相同的索引开始,我收到了这条消息

我使用相同的代码行获得了相同的错误消息,结果发现它与UICollectionView数据源无关,但是在应用程序的另一部分中有一个并行数组,它更新了其内容但不是其活动索引删除后。

要查找这样的错误, 请在删除最后一个UICollectionViewCell时启用exception断点: NSRangeException

只需反转你的两行。 您需要删除单元格才能删除对象。

 [self.collectionview performBatchUpdates:^{ [self.collectionview deleteItemsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexPath.row inSection:1]]]; [postarray removeObjectAtIndex:indexPath.item]; } completion:^(BOOL finished) {}];