【问题标题】:Crashed when deleting a row from UItableview从 UItableview 删除一行时崩溃
【发布时间】:2012-03-11 18:29:05
【问题描述】:

当使用 commitEditingStyle 从 Uitableview 中删除一行时,我的应用会因此错误而崩溃。

-[UITableView _endCellAnimationsWithContext:] 中的断言失败, /SourceCache/UIKit_Sim/UIKit-1912.3/UITableView.m:1046 .Terminating 应用程序由于未捕获的异常 NSInternalInconsistencyException', 原因:'无效更新:第 0 节中的行数无效。 更新后现有部分中包含的行数 (2) 必须等于该节之前包含的行数 更新(1),加上或减去插入或删除的行数 从该部分(0 插入,1 删除)和加号或减号 移入或移出该部分的行数(0 移入,0 移出)。

这是我的代码:

- (void)tableView:(UITableView *)tv commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    // If row is deleted, remove it from the list.
    if (editingStyle == UITableViewCellEditingStyleDelete) {
      order *OrderObj= [appDelegate.orderArray objectAtIndex:[indexPath row]];
       [appDelegate removeitem:OrderObj];

        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   }

【问题讨论】:

    标签: iphone objective-c ios uitableview


    【解决方案1】:

    尝试更改这些行:

    order *OrderObj= [appDelegate.orderArray objectAtIndex:[indexPath row]];
    [appDelegate removeitem:OrderObj];
    

    到:

    [appDelegate.orderArray removeObjectAtIndex:[indexPath row]]; // assuming orderArray is NSMutableArray
    

    【讨论】:

    • 感谢您的回复..您的回答将从 tableview 中删除该项目,但我也需要删除项目数据库..
    • @nithin。然后只需将我的行添加到您的行中而不删除:)。我猜你从数据库中删除对象,但出现崩溃是因为你在 orderArray 中的对象计数不正确
    【解决方案2】:

    您的tableView:numberOfRowsInSection: 在更新后返回错误值。检查删除前和删除后返回的值。它必须减 1。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多