我需要在UICollectionView单元格中添加一个按钮,以便用户删除它们,我尝试对按钮使用以下方法(只是测试是否能够删除单元格):
[self.customCollectionView deleteItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:6 inSection:0]]];
但我收到以下错误:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to delete item 6 from section 0 which only contains 6 items before the update
如果将indexPathForItem更改为<6(例如1),则会出现以下错误:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of items in section 0. The number of items contained in an existing section after the update (6) must be equal to the number of items contained in that section before the update (6), plus or minus the number of items inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).
有什么方法可以实现从UICollectionView中删除单元格,或者我会继续弄这些错误?
我很轻松地解决了这个问题:填充单元格的所有数据都存储在NSArray中,我只需要将其更改为NSMutableArray并在Delete按钮上添加以下方法
- (IBAction)DeleteCell:(id)sender {
//delete item from NSMutableArray
[_feedItems removeObjectAtIndex:0];
// Now delete the items from the collection view.
[self.customCollectionView deleteItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:1 inSection:0]]];
}
现在,我只需要将我要设置的按钮中的真实IndexPath传递到单元格上
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句