【问题标题】:'Invalid update: invalid number of rows in section 0'无效更新:第 0 节中的行数无效
【发布时间】:2014-02-19 03:54:33
【问题描述】:

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

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


    }
}

我认为通过让我的节数依赖于我正在编辑的数组的计数,它可以确保正确的行数?无论如何删除一行时都不必重新加载表,这不能完成吗?

【问题讨论】:

    标签: ios objective-c arrays nsmutablearray


    【解决方案1】:

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

    【讨论】:

    • 难以置信我竟然忽略了这个有缺陷的逻辑。简单的解决方案,我想弄清楚我的计数是否被错误地传递了。非常感谢。
    • 没问题!我以前无数次做过同样的事情,这就是为什么我很清楚问题出在哪里。
    • 如果我按字面意思返回行号怎么办。就像 switch (section) { case 0: return 3;休息;案例1:返回5;休息;案例2:返回2;休息; } 返回 0;我没有设置模型数组。我只是在学习表格视图。我发现了问题,我尝试创建模型数组并删除 removeObjectAtIndex 但仍然有同样的错误。
    • 为什么数组与数据源如此互锁? deleteRowsAtIndexPaths 会自动调用 reloadData 吗?然后发现不匹配?
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      就我而言,问题是 numberOfRowsInSection 在调用 tableView.deleteRows(...) 后返回的行数相似。

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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 2014-10-28
          • 1970-01-01
          • 2013-07-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-12
          • 1970-01-01
          相关资源
          最近更新 更多