在 Xcode 6 中从 UITableView 手动删除行

Deleting rows manually from UITableView in Xcode 6

我为删除按钮编写了以下代码,用于从 UITableView 中删除选定的行。

-(IBAction)deleteItems:(id)sender {

 NSArray *selectedCells = [self.autoCompleteView indexPathsForSelectedRows];
NSMutableIndexSet *indicesToDelete = [[NSMutableIndexSet alloc] init];
for (NSIndexPath *indexPath in selectedCells) {
    [indicesToDelete addIndex:indexPath.row];
}
//arrayFromPlist is NSMutableArray
[autoCompleteView beginUpdates];
[autoCompleteView deleteRowsAtIndexPaths:selectedCells withRowAnimation:UITableViewRowAnimationAutomatic];
[autoCompleteView endUpdates];
[selectedObjects removeObjectsAtIndexes:indicesToDelete];
[autoCompleteView reloadData];

[alertMsg deleteConfirmation:@"Do you want to delete these items?"];

}

请检查我的 UITableView 和删除按钮的图像。我在故事板中将 UITableview 的编辑 属性 保留为“编辑期间的多项选择”。

当我按下屏幕上显示的删除按钮时出现以下错误。

由于未捕获的异常 'NSInternalInconsistencyException',正在终止应用程序,原因:'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (5) must be equal to the number of rows contained in that section before the update (5), plus or minus the number of rows inserted or deleted from that section (0 inserted, 2 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

不确定我在删除按钮代码中的错误。

一个明确的问题是您在从模型中删除相应数据之前删除了单元格。那是错误的。永远先换机型;然后以匹配模型的方式更改单元格。