从UItableview中删除行时崩溃

nit*_*hin 2 iphone objective-c uitableview ios

使用commitEditingStyle 从Uitableview中删除行时,我的应用程序崩溃时出现此错误.

断言失败 - [UITableView _endCellAnimationsWithContext:],/ SourceCache/UIKit_Sim/UIKit-1912.3/UITableView.m:1046.由于未捕获的异常导致应用程序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];
    }   }
Run Code Online (Sandbox Code Playgroud)

Sul*_*han 6

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


ber*_*ium 6

尝试更改这些行:

order *OrderObj= [appDelegate.orderArray objectAtIndex:[indexPath row]];
[appDelegate removeitem:OrderObj];
Run Code Online (Sandbox Code Playgroud)

至:

[appDelegate.orderArray removeObjectAtIndex:[indexPath row]]; // assuming orderArray is NSMutableArray
Run Code Online (Sandbox Code Playgroud)