ios - 无效更新 : Invalid Number of Row in Section 0

标签 ios database xcode uitableview delete-row

我正在 Xcode 中处理表 UITableViewController。每次我尝试从应用程序中删除一行时,都会出现此错误:`

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (3) must be equal to the number of rows contained in that section before the update (3), 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)

表加载和删除行的代码是:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.toDoItems count];}

这就是删除方法:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {}
}

我希望你能帮我解决这个错误! 谢谢!

最佳答案

您忘记从模型 self.toDoItems 中移除对象。
尝试用这样的主体更新你的最后一个方法:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.toDoItems removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {}
    }
}

关于ios - 无效更新 : Invalid Number of Row in Section 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31010176/

相关文章:

ios - 设置 imageview 角半径

java - 如何从 MS Access 检索数据?

ios - 台风构建架构错误

objective-c - 使用 Xcode 调试库

ios - Coredata VFS 标志 - 奇怪的日志

ios - AVAssetWriter:如何多次写入单个文件

ios - 填充单元格时无法展开可选

ios - Phonegap 2.5.0 启动画面在显示期间向下移动

java - 关闭连接会自动关闭语句和结果集吗?

sql - 使用 where 进行 select 语句时,HSQLDB 用户缺少权限或找不到对象错误