【问题标题】:NSMutable array exceptionNSMutable 数组异常
【发布时间】:2014-05-17 23:32:43
【问题描述】:

我正在从 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");

【问题讨论】:

  • 我无法理解你真正想要做什么,这让我很困惑
  • if([[webservicedata valueForKey:@"id"] containsObject:[myidmutablearray objectAtIndex:indexPath.row]]) 这里给出异常 index not found 为什么会这样??
  • 简单,在按钮单击时,我使用其中一行代码来保存 id,然后在 tableview 中我将存储的 id 与来自 web 服务的 id 进行比较,明白了吗?
  • 假设表中有 20 条记录,然后单击表视图中的第 19 条数据,然后将 id 19 存储在数组中,实际上只有数组中的数据,并且您给出了表 19 的索引查看....那么显然它找不到索引
  • 数组中只有数据,是什么意思? NSmutable 数组可以相应地调整它的大小吗?

标签: ios nsmutablearray


【解决方案1】:

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-31
    • 1970-01-01
    • 2011-06-30
    • 2012-10-27
    • 2021-04-19
    • 1970-01-01
    • 2012-10-29
    相关资源
    最近更新 更多