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

问题描述:

我有一个tableview,从数组中加载数据称为self.data。 然后我试图删除行,所以第i个从数据数组中删除它,然后我试图从表视图与移除:从tableview中删除一行导致崩溃?

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).' 

尝试下面的代码TableViewDelegate

[self.tableView beginUpdates]; 

    [d removeObject:ID]; 

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

    [self.tableView endUpdates]; 
+0

感谢。这和我所做的完全一样。我没有看到这个答案如何带来新的东西。 – Curnelious

+0

您删除带ID的对象,但必须根据indexPath.row删除。 –

+0

错了。我正在移除一个带有ID的对象,因为数据库顺序与tableview顺序不同。此外,您删除的行数并不重要,但db中的行数为-1。 – Curnelious