1

删除 tableView 中的一行工作正常,直到它是最后一个现有行。然后我崩溃了:“由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无效更新:第0节中的行数无效。更新后现有节中包含的行数(1)必须等于更新前该节中包含的行数 (1),加上或减去从该节插入或删除的行数(0 插入,1 删除),加上或减去移入或移出该节的行数( 0 人搬进来,0 人搬出去)。”

我知道单元格的数量与预期的数量不同,并且在此函数中:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if categoryArray?.count == 0 {  // <<-- i imagine this line cause problem, though deleting it will not let me show "no categories made yet" and while it would be better to use "if let" statement i make "array" in realm which never is nil as its being created with 0 elements(which is not nil) 
        return 1
    }

    return categoryArray?.count ?? 1
}

问题是我如何才能以删除工作的方式实现这一点,而且我仍然可以显示一种“替换”单元格,它将显示而不是空的 tableView。

这是整个滑动删除功能:

扩展分类视图控制器:SwipeTableViewCellDelegate {

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]? {
    guard orientation == .right else { return nil }

    let deleteAction = SwipeAction(style: .destructive, title: "Delete") { action, indexPath in

        do{
            try self.realm.write{
                self.realm.delete((self.categoryArray?[indexPath.row])!)
            }
        } catch {
            print("Error deleting category\(error)")
        }
    }

    return [deleteAction]
}

任何意见,将不胜感激

4

2 回答 2

5

看起来您的问题与您更新数据源的方式有关。首先,我建议不要在您有 0 时人为地显示 1 个单元格。还有其他方法可以显示空表视图(动态添加视图以显示表为空)。话虽如此,我会在您显示的 tableView 函数中更改您的代码。

其次,如果没有看到您的其余代码,我无法判断,但看起来您正在从源中删除数据,但是您是否刷新过支持您的表视图的本地数组?如果不这样做,您将收到上述错误。那么如何解决它,对吧?

这里的关键是确保您更新支持模型的数据以及驱动表视图的本地模型。

编辑 关于如何显示空表视图的问题,请尝试以下操作:

    override func numberOfSections(in tableView: UITableView) -> Int {
    let numOfSectons: Int = 1;
    if (itemStore.allItems.count>0){
        tableView.separatorStyle = .singleLine
        tableView.backgroundView = nil
    } else{
        let noDataFrame = CGRect(x: 0, y: 0, width: tableView.bounds.size.width, height: tableView.bounds.size.height)
        let noData: UILabel = UILabel(frame: noDataFrame)
        noData.text = "No Items!"
        noData.textColor = .black
        noData.textAlignment = .center
        tableView.backgroundView = noData
        tableView.separatorStyle = .none
    }
    return numOfSectons
}

在上面的示例中,我支持 tableView 的模型是“itemStore”。这将在没有项目时自动显示,无论模型是否加载为空,或者您是否从模型中删除项目。

于 2018-08-15T21:01:08.093 回答
0

您只需要删除 TableView 中的最后一行并在域块之后重新加载您的 tableView。

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: 
IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]? {
guard orientation == .right else { return nil }

let deleteAction = SwipeAction(style: .destructive, title: "Delete") { 
action, indexPath in

    do{
        try self.realm.write{
            self.realm.delete((self.categoryArray?[indexPath.row])!)
        }
    } catch {
        print("Error deleting category\(error)")
    }
   yourTableView.deleteRows(at: indexPath.row)
   yourTableView.reloadData()
    }

    return [deleteAction]
   }
于 2021-07-26T21:45:12.933 回答