【问题标题】:Invalid Update: Invalid Number of Row in Section 0无效更新:第 0 节中的无效行数
【发布时间】:2015-06-23 18:03:33
【问题描述】:

我正在 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) {}
}

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

【问题讨论】:

    标签: ios database xcode uitableview delete-row


    【解决方案1】:

    您忘记从模型中删除对象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) {}
        }
    }
    

    【讨论】:

    • 嘿!非常感谢你!
    • 现在已修复并运行!太好了!
    猜你喜欢
    • 2014-10-28
    • 1970-01-01
    • 2013-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    相关资源
    最近更新 更多