【发布时间】: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