iphone - 删除UITableView行时为什么会发生错误?

标签 iphone objective-c ios uitableview

我正在尝试使用《 Apple Table View编程指南》中所述的标准编辑控件样式来实现UITableViewCell的标准删除。在commitEditingStyle方法中执行以下代码时

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

我收到以下错误消息

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:第2节中的行数无效。更新(1)之后现有节中包含的行数必须等于该行中包含的行数更新(1)之前的部分,加上或减去从该部分插入或删除的行数(已插入0,已删除1)。

最佳答案

如果这是唯一的行,建议您也删除该部分。您还需要将数据源与表同步,即,也删除数据源中的行。

当文件系统是您在iOS中的数据源时,这是一个正确的示例:

 // Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
 {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
    // Delete the row from the data source.
        File *file = (File *)[self.files objectAtIndex:indexPath.row];
        [[NSFileManager defaultManager] removeItemAtPath:file.path error:nil];
        [self.files removeObject:file];
        if (indexPath.row == 0 && [self.files count] == 0) {
            NSInteger sectionIndex = [indexPath indexAtPosition:0];
            [tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
        }else {
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }
        [self setupData];
    } 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.
    }   
}

关于iphone - 删除UITableView行时为什么会发生错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8391504/

相关文章:

iPhone - NSKeyedUnarchiver 内存泄漏

iphone - 从 UITableView 插入和删除行时如何正确更新数据源?

javascript - 控制 iPhone/iPad 上的视频覆盖

iphone - UIDocumentInteractionController 自 iOS6 起不起作用

ios - 在每个 View Controller 中显示用户名 jsondata

ios - OpenGL - 计算着色器 - iOS - 选项?

ios - 无法在 gdb 或 lldb 中设置观察点

ios - 新的 iOS/OSX 版本是否需要 Objective C?

ios - 将签名图像添加到 pdf 而不向 iOS 中的用户显示 pdf 数据

objective-c - 自定义步骤 UISlider