【问题标题】:iphone : proplem with table viewiphone:表格视图有问题
【发布时间】:2011-06-10 13:33:17
【问题描述】:

我在 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 = [动物 objectAtIndex:row]; 并告诉我 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSCFDictionary objectAtIndex:]:无法识别的选择器已发送到实例

【问题讨论】:

  • pListPath 的文件内容是什么?
  • 试试 NSLogging [animals objectAtIndex:row],如果不对,试试 NSLogging self.animals,如果不对,使用:NSLog(@"%@", [plistDict]);。使用它来回溯存储中的问题。代码对我来说似乎没问题。
  • IS 动物是一个数组或字典
  • 哦,pListPath 是指向 NSDictionary 的链接吗?
  • 好的,谢谢大家的快速帮助.. 看来我需要在这里睡一觉.. 但在那之前.. 谁能给我一个如何制作表格的链接,让我们说一些类别例如,在每个类别下,我们都有来自 plist 的字符串列表。我希望这对你来说很容易,再次感谢你拯救了我的一天

标签: iphone uitableview nsexception


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    [plistDict objectForKey:@"Animals"];

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        看起来 animals 是一些 dictionary 并且您正在调用 objectAtIndex: 方法。 objectAtIndex: 是NSArray 方法。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-06-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-11-09
          相关资源
          最近更新 更多