简体   繁体   中英

Removing a row from tableview causing crash?

I have a tableview that loading data from array called self.data . Then i am trying to remove a row, so first i remove it from the data array, then i am trying to remove it from the table view with :

 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];

the crash is this :

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

Try below code in TableViewDelegate

    [self.tableView beginUpdates];

    [d removeObject:ID];

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

    [self.tableView endUpdates];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM