Ios 删除具有多个节的tableview中的行会崩溃

Ios 删除具有多个节的tableview中的行会崩溃,ios,objective-c,uitableview,Ios,Objective C,Uitableview,如果所有注释都在一个部分中,我可以从tableView中删除项目。 当我将项目添加到多个部分时,从另一部分删除注释会导致应用程序崩溃,并出现以下错误: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (0) must be equal to the number of rows contained

如果所有注释都在一个部分中,我可以从tableView中删除项目。 当我将项目添加到多个部分时,从另一部分删除注释会导致应用程序崩溃,并出现以下错误:

'Invalid update: invalid number of rows in section 0.  The number of rows contained in an 
existing section after the update (0) must be equal to the number of rows contained in that
 section before the update (1), plus or minus the number of rows inserted or deleted from that
 section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of
that section (0 moved in, 0 moved out).'
有人知道这一问题的原因或解决方案吗

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

if (editingStyle == UITableViewCellEditingStyleDelete) {

    // Delete the row from the data source
    [self.tableView beginUpdates];
    [self.notes removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationRight ];

    [self.data writeToFile:self.path atomically:YES];
    [self.tableView endUpdates];

}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
    // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}

}

如果在CellForRowatineXpath中使用谓词:从数组中删除数据时应使用该谓词,因为indexath.row与数组索引不匹配。 试试这样:

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [self.tableView beginUpdates];
        if (indexPath.section == 0){
            NSPredicate *pred = [NSPredicate predicateWithFormat:@"Category =[cd] %@", @"Category 1"];
            NSArray * catArray = [self.notes filteredArrayUsingPredicate:pred];
            [self.notes removeObject:catArray[indexPath.row]]
             [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationRight ];


        } //else other section ....

        [self.data writeToFile:self.path atomically:YES];
        [self.tableView endUpdates];
    }

这个问题是否与cellForRowAtIndexPath中关于部分的问题有关:我以前看过?您是否在该方法中使用-if节和NSPredictate?是的,它们是连接的。我让cellForIndexPath与NSPredicate一起工作,它将该项添加到右侧部分,但我不使用NSPredicate删除行。如果您重新排列数据源数组以反映部分/行结构,会容易得多,例如此处或此处所建议的。请注意
[self.notes removeObject:catArray[indexPath.row]]
如果数组包含重复的元素,则可能会删除错误的元素。谢谢,但这给出了原因:“无效更新:第0节中的行数无效…谢谢Martin R,这对我来说现在不是问题hh,等等-您的意思是如果(indexath.row==0)?不,它需要是indexPath.section。请尝试在if语句中移动BeginUpdate和EndUpdate。