'无效更新:第 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

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

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)
    }
}

在我的情况下,问题是numberOfRowsInSection在调用tableView.deleteRows(...)后返回相似数量的行。

由于这是我的情况下所需的行为,因此在删除行后numberOfRowsInSection保持不变的情况下,我最终调用了tableView.reloadData()而不是tableView.deleteRows(...)

这是上面添加的一些代码,其中包含实际操作代码(第 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
}

dataModel noDataCell 为空时,我正在使用 noDataCell...

当我获得dataModel的第一行并使用以下代码更新表视图行时......

 self.tableView.performBatchUpdates({
    tableView.insertRows(at: indexPaths, with: animation)
}, completion: nil)

通过异常,行数仍然相同,因为它期望 2 numberOfRows并且仍然得到一个......

两种可能的解决方案是在 dataModel 更新后重新加载tablview或者为 noData 创建一个单独的视图,而不是使用 noDataCell

最新更新