简体   繁体   English

'无效更新:第 0 节中的行数无效

[英]'Invalid update: invalid number of rows in section 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).'

Here are the details:以下是详细信息:

in .h I have an NSMutableArray :.h我有一个NSMutableArray

@property (strong,nonatomic) NSMutableArray *currentCart;

In .m my numberOfRowsInSection looks like this:.m我的numberOfRowsInSection看起来像这样:

- (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?无论如何,在删除行时不必重新加载表就不能完成此操作吗?

You need to remove the object from your data array before you call deleteRowsAtIndexPaths:withRowAnimation: .调用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];

Swift Version --> Remove the object from your data array before you call Swift 版本 --> 在调用之前从数据数组中删除对象

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        print("Deleted")

        currentCart.remove(at: indexPath.row) //Remove element from your array 
        self.tableView.deleteRows(at: [indexPath], with: .automatic)
    }
}

In my case issue was that numberOfRowsInSection was returning similar number of rows after calling tableView.deleteRows(...) .就我而言,问题是numberOfRowsInSection在调用tableView.deleteRows(...)后返回了相似的行数。

Since this was the required behaviour in my case, I ended up calling tableView.reloadData() instead of tableView.deleteRows(...) in cases where numberOfRowsInSection will remain same after deleting a row.由于在我的情况下这是必需的行为,因此在删除行后numberOfRowsInSection将保持不变的情况下,我最终调用tableView.reloadData()而不是tableView.deleteRows(...)

Here is some code from above added with actual action code (point 1 and 2);这是上面添加的一些代码,并添加了实际操作代码(第 1 点和第 2 点);

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let deleteAction = UIContextualAction(style: .destructive, title: "Delete") { _, _, completionHandler in

        // 1. remove object from your array
        scannedItems.remove(at: indexPath.row)
        // 2. reload the table, otherwise you get an index out of bounds crash
        self.tableView.reloadData()

        completionHandler(true)
    }
    deleteAction.backgroundColor = .systemOrange
    let configuration = UISwipeActionsConfiguration(actions: [deleteAction])
    configuration.performsFirstActionWithFullSwipe = true
    return configuration
}

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

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