【问题标题】:insertRowsatIndexPath exception iOSinsertRowsatIndexPath 异常 iOS
【发布时间】:2013-11-07 17:02:59
【问题描述】:

* 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“尝试将第 0 行插入第 0 部分,但更新后第 0 部分中只有 0 行”

我意识到这里的问题是我试图向 tableView 添加一个单元格,但它不希望它不添加一个单元格,我不知道为什么。

- (IBAction)addNewIngredient: (id) sender
{
    NSString *newIngredient = [recipe createIngredient];
    [[recipe ingredients]addObject:newIngredient];

    //figure out where that item is in the array
    int lastRow = [[recipe ingredients]indexOfObject: newIngredient];

    NSIndexPath *ip = [NSIndexPath indexPathForRow:lastRow inSection:0];

    NSLog(@"Added a new ingredient!");

    [[self tableView] insertRowsAtIndexPaths:[NSArray arrayWithObject:ip] withRowAnimation:
     UITableViewRowAnimationTop];


}

为了澄清我在这里尝试做的事情:

recipe 有一个属性成分,它是一个 NSStrings 数组。 我正在向成分数组添加一个对象(这会自动增加数组计数还是我必须手动执行?)...

然后我用“int lastRow = [[recipe ingredients]indexOfObject:newIngredient]”找出该项目在数组中的位置 然后获取指向那里的索引路径。然后将该成分插入到 tableView 中。

【问题讨论】:

  • 什么是recipe?它存在吗?

标签: ios insert rows


【解决方案1】:

您出现异常的原因是当您插入该行时,您的numberOfRowsInSection 将被再次调用。

此时,您的数据需要更新,如果您有 0 行并且正在向其中插入 1 行,则该函数返回 1

【讨论】:

  • 好吧,这是有道理的。我的问题是,当我将Object:newIngredient 添加到我的数组时,这不会将数据计数增加1 吗?
  • 嗯,是的,但是您在numberOfRowsInSection 中为第 0 部分检查的数组是否相同?仔细调试,你会发现错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-07
相关资源
最近更新 更多