我已经阅读了所有与此相关的文章,但仍然出现错误:

'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, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

详细信息如下:

.h中,我有一个NSMutableArray:
@property (strong,nonatomic) NSMutableArray *currentCart;

.m中,我的numberOfRowsInSection看起来像这样:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.


    return ([currentCart count]);

}

要启用删除并从阵列中删除对象,请执行以下操作:
// Editing of rows is enabled
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {

        //when delete is tapped
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

        [currentCart removeObjectAtIndex:indexPath.row];


    }
}

我以为,通过让我的节数依赖于我正在编辑的数组的数量,可以确保适当的行数?在删除行时是否不必重新加载表就可以做到这一点吗?

最佳答案

在调用deleteRowsAtIndexPaths:withRowAnimation:之前,需要从数据数组中删除对象。因此,您的代码应如下所示:

// Editing of rows is enabled
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {

        //when delete is tapped
        [currentCart removeObjectAtIndex:indexPath.row];

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

您还可以使用数组创建快捷方式@[]稍微简化代码:
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

关于ios - '无效的更新: invalid number of rows in section 0,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21870680/

10-11 13:43