如何删除UICollectionView单元格

时间:2015-03-06 11:19:05

标签: objective-c uicollectionview

我需要在我的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中删除单元格,或者我会继续这些错误?

1 个答案:

答案 0 :(得分:0)

我很容易解决这个问题: 填充我的单元格的所有数据都存储在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