【问题标题】:UITableView NSMutableArray and NSDictionaryUITableView NSMutableArray 和 NSDictionary
【发布时间】:2014-05-24 12:08:24
【问题描述】:

从 nsmutablearray 填充 tableview 时出现此错误。

将数组添加到字典中,将字典添加到可变数组中,但是每次加载视图时应用都会崩溃并出现错误:

[__NSArrayI isEqualToString:]: 无法识别的选择器发送到实例 0x8a87e60 2014-05-24 12:23:02.589 jwStudy[77652:907] * 终止 应用程序由于未捕获的异常“NSInvalidArgumentException”,原因: '-[__NSArrayI isEqualToString:]: 无法识别的选择器发送到 实例 0x8a87e60'

日志输出确认数组和键添加正确。 但是为什么 cellForRowAtIndexPath: 方法中的单元格会抛出错误? 代码如下:

- (void)viewDidLoad
{
    [super viewDidLoad];


    languageArray = [[NSMutableArray alloc] init];
    languages = [[NSArray alloc] initWithObjects:@"Abbey", @"Abkhasisk", @"Abua", nil];

    downloadURL = [[NSArray alloc] initWithObjects:@"http://download.jw.org/files/content_assets/bb/502013341_ABB_cnt_1_r480P.mp4",
                   @"http://download.jw.org/files/content_assets/3c/502013341_ABK_cnt_1_r480P.mp4",
                   @"http://download.jw.org/files/content_assets/7e/502013341_AU_cnt_1_r720P.mp4", nil];

    mutedict = [NSDictionary dictionaryWithObjectsAndKeys:languages, @"FirstKey", downloadURL, @"SecondKey", nil];
    [languageArray addObject:mutedict];

   NSLog(@"%@",languageArray);

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

        return languageArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *simpleTableIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    // ERROR HERE!!!
    cell.textLabel.text =  [[languageArray objectAtIndex:indexPath.row] objectForKey:@"FirstKey”];

    if ([checkCellLanguage isEqual:indexPath]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    return cell;
}

【问题讨论】:

  • 您的 checkCellLanguage 是 NSInteger 类型的吗?另外,objectForKey 方法只能在 NSDictionary 的实例上调用。您应该将数组对象存储到字典中,而不是将字典存储到数组中。之后,您可以在 NSDictionary 对象上使用 objectForKey。
  • 您可以忽略 checkCellLanguage 变量。这只是为所选行添加复选标记。我对以正确的方式存储数组对象有点困惑。你能详细说明一下吗?
  • 当然。您的 arrayLanguage 变量超出范围,因为它仅在索引 0 处分配(整个 *mutedict)一个对象。您可以使用 for 循环分配字典,其中循环的限制可以是您想要的数组的大小分配。你可以看到这个link。它可以提供帮助。
  • 谢谢法尔汉。我去看看。

标签: uitableview nsmutablearray nsdictionary


【解决方案1】:

@Farhan 和乔伊。

感谢两位的及时回复。我现在意识到我的问题不够清楚。我想要的是为 tableview 中的每个选定行注册该行的相应 URL。所以我最终放弃了数组和字典......太混乱了。而是使用 2 个 NSArrays。这个链接让我走上了正确的道路,现在一切正常:

http://sweettutos.com/2012/08/25/loading-urls-from-uitableview/

【讨论】:

    【解决方案2】:

    在您的代码中,两个数组存储在一个字典中,而字典存储在一个数组中。

    像这样:

    languageArray ----- mutedict ------ languages (FirstKey)
    
                           |
                           |
                            ----------- downloadURL(SecondKey)
    

    cell.textLabel.textNSString,但 [[languageArray objectAtIndex:indexPath.row] objectForKey:@"FirstKey”];NSArray

    所以你的代码会抛出一个错误。

    如果你想把language 写成cell.textLabel.text

    您可以将您的代码修改为cell.textLabel.text = [[mutedict objectForKey:@"FirstKey"] objectAtIndex:indexPath.row]

    希望对你有帮助:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-07
      • 2011-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多