简体   繁体   English

检查NSMutableArray元素是否存在

[英]Check NSMutableArray element is exist

I get the data from the network. 我从网络上获取数据。 In ViewDidLoad method I init and retain my array: 在ViewDidLoad方法中,我初始化并保留了我的数组:

arrBannersIMG = [[NSMutableArray alloc]init];
 [arrBannersIMG retain];

In CellForRowAtIndexPath I need to check if arrBannersImg[indexPath.row] is exist, if YES - set cell.image from this array, if not - init UIImage by data from Internet, and add this image to arrBannersIMG. 在CellForRowAtIndexPath中,我需要检查arrBannersImg[indexPath.row]是否存在,如果是-从该数组设置cell.image,否则-通过Internet数据初始化UIImage,并将此图像添加到arrBannersIMG。 I tried this: 我尝试了这个:

 if (_arrayOfImages[indexPath.row] == [NSNull null]) {
       //
}

But I have error on this line: 但是我在这条线上有错误:

Terminating app due to uncaught exception 'NSRangeException', reason: ' * -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array' 由于未捕获的异常'NSRangeException'而终止应用程序,原因:' * -[__ NSArrayM objectAtIndex:]:索引0超出了空数组的范围'

I can check array like this: 我可以这样检查数组:

if (!array || !array.count){
  ...
}

But I need to check current element, not all array. 但是我需要检查当前元素,而不是所有数组。 How can I do this? 我怎样才能做到这一点?

You should probably change your approach and leverage some open source code. 您可能应该改变方法,并利用一些开源代码。 You don't want to be using a sparsely populated array really (fast scrolling could cause you issues). 您实际上不想使用人口稀疏的数组(快速滚动可能会导致问题)。 You also want to be sure to download images in the background. 您还希望确保在后台下载图像。

Look at using SDWebImage and just storing all of the image URLs in your array. 看一下使用SDWebImage并将所有图像URL存储在数组中。 Now, when you need to display the image just pass the URL to SDWebImage and it will load from the cache / internet as required for you. 现在,当您需要显示图像时,只需将URL传递到SDWebImage ,它将根据您的需要从缓存/互联网加载。

I'm not sure why you want look elements inside a empty array but to solve this error, try that : 我不确定为什么要在空数组中查找元素,但是要解决此错误,请尝试以下操作:

if(array.count > indexPath.row) {
    if (_arrayOfImages[indexPath.row] == [NSNull null]) {
        // ...
    }
}

Hope that will help. 希望对您有所帮助。

Firstly I would suggest you that as per your condition you should work with arrays out of CellForRowAtIndexPath method. 首先,我建议您根据条件使用CellForRowAtIndexPath方法之外的数组。

This is because may be your array is already empty and you are trying to compare it with indexpath.row that is not exist. 这是因为可能是您的数组已经为空,并且您正在尝试将其与不存在的indexpath.row进行比较。 Thats why its crashed. 这就是为什么它崩溃了。

Firstly you should check that _arrayOfImages has any count or not. 首先,您应该检查_arrayOfImages是否具有任何计数。

_arrayOfImages=arrBannersIMG;

if([_arrayOfImages count]>0)
{ 
 NSData *imgData = UIImageJPEGRepresentation([_arrayOfImages indexPath.row], 0);
UIImage  *img = [UIImage imageWithData:[_arrayOfImages indexPath.row];
}
else
{
 //init UIImage by data from Internet
arrBannersIMG= //Insert your image object in array
}

I don't know your whole scenario , but this kind of array implementation will save us by crash if we access it out of delegate methods like CellForRowAtIndexPath. 我不知道您的整个情况,但是如果我们从像CellForRowAtIndexPath这样的委托方法中访问它,这种数组实现将使我们免于崩溃。

Hope it helps. 希望能帮助到你。 please check and let me know if we have to go with another solution. 请检查,并让我们知道是否必须使用其他解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM