【问题标题】:Swipe to delete rows with multi section in tableview?滑动以删除表格视图中具有多节的行?
【发布时间】:2020-02-27 11:55:14
【问题描述】:

我有一个包含两个部分的表格视图

我添加了滑动删除行

但应用崩溃导致选择当前indexPath出错

我尝试了两种不同的方法,但都不起作用

//the code
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let contextItem = UIContextualAction(style: .destructive, title: "Delete") {  (contextualAction, view, boolValue) in
        switch indexPath.section{
        case 0:
            //1 i tried this
            self.tableView.deleteRows(at: [indexPath], with: .automatic)
        case 1:
            //2 and i tried this
            self.tableView.deleteRows(at: [IndexPath(row: indexPath.row, section: 1)], with: .automatic)
        default:break
        }

        boolValue(true)
    }
    let swipeActions = UISwipeActionsConfiguration(actions: [contextItem])
    return swipeActions
}

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return true
}

结果说:“由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无效更新:第1节中的行数无效。更新后现有节中包含的行数(5)必须相等更新前该节中包含的行数 (5),加上或减去从该节插入或删除的行数(0 插入,1 删除),加上或减去移入或移出该节的行数部分(0 移入,0 移出)。'"

【问题讨论】:

  • 首先从给定索引处的数据源数组中删除项目,然后调用deleteRows
  • 我试过了,谢谢

标签: ios swift uitableview tableview


【解决方案1】:

由于未捕获的异常而终止应用 'NSInternalInconsistencyException',原因:'无效更新:无效 第 1 节中的行数。包含在第 1 节中的行数 更新后的现有节(5)必须等于 更新之前该部分中包含的行 (5),加号或减号 从该部分插入或删除的行数(0 插入, 1 已删除)并加上或减去移入或移出的行数 该部分(0 移入,0 移出)。'

它说明了你必须做的确切事情。请记住,删除数据时,您需要使您的 DATASOURCE 计数等于 方法 deleteRowsdeleteSections 之后的行数和节数。

这意味着您在调用这些方法之前操作您的数据源数组。在使用多节时,请记住通过sectionrow 以及在删除和插入数据时小心访问您的数据源。

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {

        let contextItem = UIContextualAction(style: .destructive, title: "Delete") {  (contextualAction, view, boolValue) in

            let section = indexPath.section
            let row = indexPath.row

            self.data[section].remove(at: row)
            self.tableView.deleteRows(at: [indexPath], with: .automatic)

            boolValue(true)
        }

        let swipeActions = UISwipeActionsConfiguration(actions: [contextItem])


        return swipeActions
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-24
    • 1970-01-01
    • 2016-01-20
    • 1970-01-01
    • 2019-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多