删除集合视图上单击的单元格

时间:2014-03-12 17:23:25

标签: ios

当我运行我的代码时,我的集合视图中有一些单元格,现在我尝试点击哪一个并删除单击的单元格,但每次我的数组上的第一个单元格被删除! 这是我的鳕鱼:

- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    // 1 = Tapped yes
    if (buttonIndex == 1)
    {
        // ....

        NSIndexPath *indexPath = [MainCollectionView indexPathForCell:MainCollectionView];
        UICollectionViewCell *cell = [MainCollectionView cellForItemAtIndexPath:indexPath];
         [cellImages removeObjectAtIndex:indexPath.row];
        [MainCollectionView reloadData];
    }
}

我认为问题出在我的indexPath上,有人会告诉我出了什么问题吗?

1 个答案:

答案 0 :(得分:0)

您可能想要添加一个变量来存储所点击视图的indexPath?

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    _clickedIndexPath = indexPath;
    //Show AlertView
}

- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        [cellImages removeObjectAtIndex:_clickedIndexPath.row];
        [_collectionView deleteItemsAtIndexPaths:@[_clickedIndexPath]];
    }
}

或者存储clickedCell,使用:NSIndexPath *indexPath = [_yourCollectionView indexPathForCell:_theClickedCell];