【问题标题】:delete table cell from UITableViewController causes crash?从 UITableViewController 中删除表格单元格会导致崩溃?
【发布时间】:2015-03-26 16:17:00
【问题描述】:

我在我的 iOS 应用程序中添加了以下代码以从 UITableViewController 中删除表格单元格,但我收到有关删除的错误。

  -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if(editingStyle == UITableViewCellEditingStyleDelete){
        [self.toDoItems removeObjectAtIndex:indexPath.row];
        NSArray *indexPaths             = @[indexPath];

        [self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
    }

}

更新: toDoItems 只是一个 NSArray,我尝试了以下代码但仍然无法正常工作:

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if(editingStyle == UITableViewCellEditingStyleDelete){
        NSArray *indexPaths             = @[indexPath];

        [self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
        [self.toDoItems removeObjectAtIndex:indexPath.row];
    }

}

错误信息在这里:

'NSInternalInconsistencyException',原因:'无效更新:第 1 节中的行数无效。更新 (1003) 后现有节中包含的行数必须等于该节中包含的行数之前更新(1004),加上或减去从该节插入或删除的行数(0 插入,0 删除),加上或减去移入或移出该节的行数(0 移入,0 移出)。 '

Xcode 6.2 iOS6

我已经尝试过Deleting table cell causes crash

【问题讨论】:

  • 您是否尝试过将 tableView 删除代码移到 toDoItems 删除代码上方?如果您的 toDoItems 与核心数据相关,则操作将导致在删除之前刷新表。
  • @Travis M 查看更新

标签: ios uitableview ios6


【解决方案1】:

问题是您从数据模型中删除了错误的对象:

[self.toDoItems removeObjectAtIndex:indexPath.row];

这太简单了。此表中有多个部分。您需要一个区分这些部分的数据模型,并且您需要在数据模型中从正确的部分中删除正确的行,然后再从表中删除相应的行。您不能仅通过查看indexPath.row 来做到这一点;你需要构建你的数据模型,以便同时考虑indexPath.section

【讨论】:

  • 这是一个工作示例,演示了从具有多个部分的表中删除一行:github.com/mattneub/Programming-iOS-Book-Examples/blob/master/… 请注意,数据模型是一个数组数据对象的数组。因此,要从模型中删除正确的对象,我调用[self.sectionData[ip.section] removeObjectAtIndex:ip.row] - 请注意,这涉及both 部分和行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-22
  • 2018-02-06
相关资源
最近更新 更多