Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios iPhone应用程序在UICollectionView的DidDecelectItemAtIndexPath中崩溃_Ios_Objective C_Iphone_Nsmutablearray_Uicollectionview - Fatal编程技术网

Ios iPhone应用程序在UICollectionView的DidDecelectItemAtIndexPath中崩溃

Ios iPhone应用程序在UICollectionView的DidDecelectItemAtIndexPath中崩溃,ios,objective-c,iphone,nsmutablearray,uicollectionview,Ios,Objective C,Iphone,Nsmutablearray,Uicollectionview,我已经在我的应用程序中实现了UICollectionView,当我从UICollectionView中取消选择图像时,我将从NSMutableArray中删除该UIImage。现在我的问题是,如果图像多于两个,并且我正在取消选择该图像,那么我的应用程序将崩溃 而获得错误是非常困难的 由于未捕获异常“NSRangeException”而终止应用程序,原因:“***-[\uu NSArrayM removeObjectAtIndex:]:索引2超出边界[0..1]” 下面是我的代码 - (void)

我已经在我的应用程序中实现了UICollectionView,当我从UICollectionView中取消选择图像时,我将从NSMutableArray中删除该UIImage。现在我的问题是,如果图像多于两个,并且我正在取消选择该图像,那么我的应用程序将崩溃

而获得错误是非常困难的

由于未捕获异常“NSRangeException”而终止应用程序,原因:“***-[\uu NSArrayM removeObjectAtIndex:]:索引2超出边界[0..1]”

下面是我的代码

- (void)collectionView:(UICollectionView *)collectionView 
        didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [selectedImagesArray removeObjectAtIndex:indexPath.row];
}

我得到的是超出该数组的indexPath。

添加这行代码以处理检查:

if ([indexPath.row] < [selectedImagesArray count]) {

    [selectedImagesArray removeObjectAtIndex:indexPath.row];
}

IndexPath不一定有row属性,因此可能是nil。如果为nil,您将看到崩溃

检查数组计数是否大于indexPath.row属性,然后删除或检查该索引是否存在数组元素,如果是,则删除该元素

if(selectedImageArray[indexPath.row] != nil) {
   [selectedImagesArray removeObjectAtIndex:indexPath.row];
}
index.xpath.row将返回实际数组的索引,而不是所选数组索引。 因此,您必须在SelectedArray中找到相同的元素,然后才能从所选阵列中删除该图像

比如说

实际数组对象

[1,2,3,4,5,6,7,8,9,10]

从中选择对象

[1,5,7]

因此,对象[1,5,7]将处于选定状态,并且也处于选定阵列中

当您选择对象7时,Indexpath.row将返回6


您正在直接从SelectedArray中删除索引6,该数组不可用,因此出现此错误。

您必须使用indexPath.item。 这通常在collectionView中使用。 IndexPath.row用于TableView


无论如何,在其周围放置一个if条件,以检查此索引是否存在。

您在didSelect上做了什么?