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

Ios 删除集合视图上单击的单元格,ios,Ios,当我运行我的代码时,我的集合视图中有几个单元格,现在我尝试单击其中一个并删除单击的单元格,但每次我数组中的前一个单元格被删除!!! 这是我的cod: - (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { // 1 = Tapped yes if (buttonIndex == 1) { // .... NSInde

当我运行我的代码时,我的集合视图中有几个单元格,现在我尝试单击其中一个并删除单击的单元格,但每次我数组中的前一个单元格被删除!!! 这是我的cod:

- (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上,有人能告诉我出了什么问题吗???

您可能想添加一个变量来存储单击视图的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=[\u yourCollectionView indexPathForCell:\u clickedCell]

[MainCollectionView indexPathForCell:MainCollectionView]
意思?我用这段代码尝试在我的uicollectionview(MainCollectionView)上选择单击的单元格。该方法称为
indexPathForCell
,并期望在末尾有一个单元格,您将给出uicollectionview。这就是indexath返回0,0的原因。改用
indexPathsForSelectedItems