簡體   English   中英

從tableview中刪除一行會導致崩潰?

[英]Removing a row from tableview causing crash?

我有一個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).'

TableViewDelegate嘗試以下代碼

    [self.tableView beginUpdates];

    [d removeObject:ID];

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

    [self.tableView endUpdates];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM