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

标签 ios objective-c uitableview

我有一个 TableView ,它从名为 self.data 的数组中加载数据。 然后我试图删除一行,所以首先我将它从数据数组中删除,然后我试图从 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];

关于ios - 从 tableview 中删除一行导致崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32795024/

相关文章:

uitableview - TableView 多段

ios - 为什么 tableViewCell 在返回到 ViewController 时未被选中?

ios - 自定义 tableViewCell 标签不显示

ios - 望远镜应用程序无法通过自定义 URL 进行移动响应

ios - Xcode UI 测试按钮未点击

ios - 如何使 UIBarButtonItem 不可见但有效?

iphone - 为什么我的if语句不触发?

iphone - 如何在一个viewClass中通知另一个View类

ios - 使用 Monotouch 检测 iPhone 上的慢速互联网

ios - 拆除 UITableViewDataSource