从tableview中删除一行导致崩溃?

时间:2015-09-26 08:09:43

标签: ios objective-c uitableview

我有一个tableview,从数组加载数据self.data。 然后我试图删除一行,所以首先我从数据数组中删除它,然后我试图从表视图中删除它:

 Data *d=[[Data alloc] init];
    [d removeObject:ID];

    NSLog(@"%ld",editingRow); //the right row to remove

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:editingRow inSection:0];


     //update table
     [self.tableView beginUpdates];
      [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
     [self.tableView endUpdates];

崩溃是这样的:

Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

1 个答案:

答案 0 :(得分:1)

TableViewDelegate

中尝试以下代码
    [self.tableView beginUpdates];

    [d removeObject:ID];

    [self.tableView deleteRowsAtIndexPaths:[NSIndexPath indexPathForRow:ID inSection:0]
                  withRowAnimation:UITableViewRowAnimationRight];

    [self.tableView endUpdates];