'无效更新:第 0 节中的行数无效 [英] 'Invalid update: invalid number of rows in section 0

查看:34
本文介绍了'无效更新:第 0 节中的行数无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了所有与此相关的帖子,但仍然有错误:

I've read all of the related posts regarding this and I am still having an error:

'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 看起来像这样:

In .m my numberOfRowsInSection looks like this:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.


    return ([currentCart count]);

}

启用从数组中删除和移除对象:

To enable delete and remove the object from the array:

// 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];


    }
}

我认为通过让我的部分数量依赖于我正在编辑的数组的计数,它会确保正确的行数吗?无论如何,在删除行时不必重新加载表就可以完成此操作吗?

I thought that by having my number of sections relying on the count of the array I'm editing it would ensure the proper number of rows? Can't this be done without having to reload the table when you delete a row anyway?

推荐答案

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

You need to remove the object from your data array before you call deleteRowsAtIndexPaths:withRowAnimation:. So, your code should look like this:

// 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];
    }
}

您还可以使用数组创建快捷方式@[]:

You can also simplify your code a little by using the array creation shortcut @[]:

[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

这篇关于'无效更新:第 0 节中的行数无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆