如何删除 UICollectionView 单元格

How to remove UICollectionView cells

我需要在我的 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 并在删除按钮上添加以下方法

- (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 从我要设置的按钮传递到单元格上