我在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:]:无法识别的选择器发送到实例
答案 0 :(得分:2)
plist中的Animals
键指的是字典,而不是数组。字典没有保证的顺序,因此要求特定索引处的对象没有意义。
除此之外,还有内存泄漏 - plistDict
已分配但从未发布。您是否在代码上运行静态分析器?
答案 1 :(得分:0)
[plistDict objectForKey:@"Animals"];
返回的字典不是您期望的数组。您需要检查plist文件以查看数据是否正确。
答案 2 :(得分:0)
错误似乎是你在线路上的NSDictionary对象上调用objectAtIndex
.textLabel.text = [animals objectAtIndex:row];检查动物在运行时包含的内容。为此,请在此行之前使用NSLog。的NSLog(@ “%@”,动物);
答案 3 :(得分:0)
看起来animals
有些dictionary
,您正在调用objectAtIndex:
方法。 objectAtIndex
:NSArray
方法。