简体   繁体   English

从表中删除行时出错

[英]Error deleting row from table

I'm having a very strange error that happens when I try to delete a row from my table. 当我尝试从表中删除一行时,发生了一个非常奇怪的错误。 The error I get is: 我得到的错误是:

Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), 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).

I've read this is usually an error when someone doesn't remove the object from their data source before calling deleteRowsAtIndexPaths. 我读过这通常是一个错误,因为有人在调用deleteRowsAtIndexPaths之前没有从数据源中删除该对象。 However, I'm doing this... Here is my code: 但是,我正在这样做...这是我的代码:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
         /* Remove from DB */
        [dbHandler deleteRow:indexPath];
        /* Removing row from data source */
        [[self.listData objectAtIndex:indexPath.section] removeObjectAtIndex:indexPath.row];
        /* Remove row from table - It fails on this line... */
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation: UITableViewRowAnimationTop];
     }
}

When I step through this in a debugger, it certainly deletes that object from the datasource. 当我在调试器中逐步执行此操作时,它肯定会从数据源中删除该对象。 Is there something else I need to be doing? 还有什么我需要做的吗?

I figured it out... I was adding an extra row when to my table when there were more rows to load than were already rendered on the screen. 我弄清楚了...当要加载的行比屏幕上已经呈现的行多时,我在表中添加了一行。 Similar to how the Mail app does the "Load more" feature... I wasn't taking into account this extra cell when I deleted them. 类似于邮件应用程序执行“加载更多”功能的方式...删除它们时,我没有考虑到这个多余的单元格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM