在uitableView中删除行时,ios应用程序崩溃

bra*_*imm 1 uitableview ios swift

我的UITableView中有一个问题data: [(gamme: String, [(product: String, quantity: Double)])],一切正常:插入行和部分,删除行和部分,重新​​加载.但有时当我尝试以快速方式删除许多行时(通过滑动表格并点击( - )逐行).它会导致像屏幕截图中的崩溃一样.

这个问题很难在开发应用程序中重现.但我的客户仍然报告它.我的客户是专业人士(不是普通用户),并希望以快速的方式使用中型到大型数据.

在此输入图像描述

这是我删除行的函数:

    override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {

    let delete = UITableViewRowAction(style: .destructive, title: "-") { (action, indexPath) in

        let cmd = self.groupedData[indexPath.section].1.remove(at: indexPath.row)
        tableView.deleteRows(at: [indexPath], with: .right)
        self.delegate?.didDeleteCmdLine(cmd)

        if self.groupedData[indexPath.section].1.count == 0 {
            self.groupedData.remove(at: indexPath.section)
            tableView.deleteSections(IndexSet(integer: indexPath.section), with: UITableViewRowAnimation.right)
        }
    }

    return [delete]
}
Run Code Online (Sandbox Code Playgroud)

为什么会这样?

这是崩溃的xcode组织者屏幕

在此输入图像描述 编辑:

检查是否groupedData由@Reinhard提出的主要线程以外的任何线程访问:

private var xgroupedData = [(gamme: GammePrdCnsPrcpl, [cmdline])]()

private var groupedData: [(gamme: GammePrdCnsPrcpl, [cmdline])] {
    get {
        if !Thread.isMainThread {
            fatalError("getting from not from main")
        }
        return xgroupedData
    }
    set {
        if !Thread.isMainThread {
            fatalError("setting from not from main")
        }
        xgroupedData = newValue
    }
}
Run Code Online (Sandbox Code Playgroud)

但是groupedData只能从主线程访问该变量

Ali*_*RAL 5

tableView.beginUpdates()
self.tableView.deleteRows(at: [indexPath], with: .automatic)
tableView.endUpdates()
Run Code Online (Sandbox Code Playgroud)