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

时间:2014-02-19 03:54:34

标签: ios objective-c arrays nsmutablearray

我已阅读有关此内容的所有相关帖子,但我仍然遇到错误:

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


    }
}

我认为通过让我的部分数量依赖于我正在编辑的数组的数量,它会确保正确的行数?无论如何在删除行时不必重新加载表就不能这样做吗?

4 个答案:

答案 0 :(得分:145)

在调用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];

答案 1 :(得分:4)

快速版本->在调用

之前从数据数组中删除对象
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)
    }
}

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

以下是一些上面添加了实际操作代码的代码(第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
}