iphone:proplem与表视图

问题描述:

我有在iphone表视图问题..我想不通为什么它崩溃每次 将在这里是代码iphone:proplem与表视图

- (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 [email protected]"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; 
} 

它的崩溃在该行 细胞.textLabel.text = [动物objectAtIndex:row]; 并告诉我, 终止应用程序由于未捕获的异常“NSInvalidArgumentException”,原因是:“ - [__ NSCFDictionary objectAtIndex:]:无法识别的选择发送到实例

+0

pListPath中文件的内容是什么? – saadnib 2011-06-10 13:36:08

+0

动物是否有字典? – 2011-06-10 13:38:24

+0

尝试NSLogging'[animals objectAtIndex:row]',如果它是错误的,请尝试NSLogging'self.animals',如果这是错误的,请使用:'NSLog(@“%@”,[plistDict]);'。用它来回溯存储中的错误。代码对我来说似乎没问题。 – Joetjah 2011-06-10 13:38:41

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

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

[plistDict objectForKey:@"Animals"];

返回一个字典不是数组像你期望。你需要检查你的plist文件,看看数据是否正确。

这个错误似乎是你在线上的NSDictionary对象上调用objectAtIndex cell.textLabel.text = [animals objectAtIndex:row];检查动物在运行时包含什么。为此在这一行之前使用NSLog。的NSLog(@ “%@”,动物);

看起来像animals是一些dictionary,你打电话给objectAtIndex:方法。 objectAtIndex:是NSArray方法。