NSMutable数组异常

时间:2014-05-17 06:39:27

标签: ios nsmutablearray

我从Web服务获取数据并将其显示在tableview中,并在按钮点击时在tableview上我将数据id存储在nsmutablearray中。然后我将存储在nsmutable数组中的点击数据id与cellForRowAtIndexPath函数中的Web服务数据进行比较。

错误:

在tableview中的代码比较中,找不到异常索引。

按下按钮我尝试了所有这些::::

 [myidmutablearray addObject:[[webservicedata valueForKey:@"id"]objectAtIndex:clickedButtonIndexPath.row]]; 

 [myidmutablearray insertObject:[webservicedata valueForKey:@"id"] atIndex:clickedButtonIndexPath.row];

 [myidmutablearray replaceObjectAtIndex:clickedButtonIndexPath.row withObject:[webservicedata valueForKey:@"id"]];

在CellForRowAtIndexPath我尝试了这个,

    if([myidmutablearray count]!=0)

    {
    if([[webservicedata valueForKey:@"id"] containsObject:[myidmutablearray objectAtIndex:indexPath.row]])

        NSLog(@"not empty");
    }

  else
        NSLog(@"empty");

1 个答案:

答案 0 :(得分:0)

NSMutableArray不是具有无限数量条目的数组,您可以指定其索引。

在向NSMutableArray添加对象时,添加的第一个条目将在索引0处。无论您在添加时为其提供什么索引。

我认为你真正想要的是一个NSDictionary,其密钥是由indexpath.row创建的NSNumber。然后,您可以稍后再次使用NSNumber检索它。

保存时,请执行以下操作:

//defining somewhere 
NSMutableDictionary *myIdMutableDictionary;


//then in the button click:
[myIdMutableDictionary setObject:[webservicedata valueForKey:@"id"] forKey:@(indexPath.row)];


//then later to check it, you test value would be in this:
[[myIdMutableDictionary objectForKey:@(indexPath.row)
相关问题