0

我在 iphone 中的表格视图有问题.. 我不知道为什么它每次都会崩溃,这是代码

   - (void)viewDidLoad
{
     [self checkAndCreatePList];
    NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:pListPath];

    self.animals = [plistDict objectForKey:@"Animals"];



         [super viewDidLoad];

}



    -(UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath
{
    static NSString *SimpleTableIdentifier =@"SimpleTableIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
    if(cell== nil){
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SimpleTableIdentifier]autorelease];
    }
    NSUInteger row = [indexPath row];

    cell.textLabel.text = [animals objectAtIndex:row];

    return cell;
}

它在 cell.textLabel.text = [animals objectAtIndex:row]; 行崩溃 并告诉我由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance

4

4 回答 4

2

plist 中的Animals键是指字典,而不是数组。字典没有保证顺序,因此在特定索引处请求对象是没有意义的。

除此之外,您还有内存泄漏 -plistDict已分配但从未释放。您是否在代码上运行过静态分析器?

于 2011-06-10T13:38:42.677 回答
0

[plistDict objectForKey:@"Animals"];

正在返回字典而不是您期望的数组。您需要检查您的 plist 文件以查看数据是否正确。

于 2011-06-10T13:39:33.810 回答
0

该错误似乎是您objectAtIndex在 cell.textLabel.text = [animals objectAtIndex:row]; 行调用 NSDictionary 对象 检查动物在运行时包含什么。为此,请在此行之前使用 NSLog。NSLog(@"%@",动物);

于 2011-06-10T13:39:42.207 回答
0

看起来animals是一些dictionary,你正在调用objectAtIndex:它的方法。objectAtIndex: 是NSArray方法。

于 2011-06-10T13:40:07.740 回答