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

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

问题描述

我读过所有关于此相关的帖子和​​我仍然有一个错误:

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

无效的更新:在第0行无效数字包含在更新后,现有部分的行数(5)必须更新(5)前等于包含在该节的行数,加或减号插入或从该段(0插入,删除1),并加上或减去行数删除的行数移入或移出该条第(0搬进来,0迁出)。

'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:

in my .h I have an NSMutableArray:

@property (strong,nonatomic) NSMutableArray *currentCart;

在我的.m我numberOfRowsInSection看起来是这样的:

In my .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:。所以,你的code应该是这样的:

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

您还可以使用数组创建快捷方式简化您的code稍微 @ []

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

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

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

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