繁体   English   中英

Swift-在tableView.deleteRows上崩溃(在:[selectedIndexPath],带有:.automatic)

[英]Swift - Crashing on tableView.deleteRows(at: [selectedIndexPath], with: .automatic)

尝试从表格视图中删除单元格时,如何解决此崩溃问题?

调试器在该行显示错误: tableView.deleteRows(at:[selectedIndexPath],带有:.automatic)

带有以下消息:* 2018-02-05 23:13:42.355696-0800 Invoice [66511:19435120] ***-[UITableView _endCellAnimationsWithContext:],/ BuildRoot / Library / Caches / com.apple.xbs / Sources中的断言失败/UIKit_Sim/UIKit-3698.33.6/UITableView.m:2011*

数据是从返回json的服务器加载的。 添加,加载和更新工作正常。

@IBAction func unwindToClients(sender: UIStoryboardSegue) {

    if let sourceViewController = sender.source as? ClientViewController,
        let client = sourceViewController.client,
        let wasDeleted = sourceViewController.wasDeleted {

        if(wasDeleted) {
            if let selectedIndexPath = tableView.indexPathForSelectedRow {
                print("Delteted")
                tableArray.remove(at: selectedIndexPath.row)
                tableView.deleteRows(at: [selectedIndexPath], with: .automatic)
            }

        }
        else {
            if let selectedIndexPath = tableView.indexPathForSelectedRow {
                // Update an existing client.
                tableArray[selectedIndexPath.row] = client
                tableView.reloadRows(at: [selectedIndexPath], with: .automatic)
            }
            else {
                // Add a client.
                tableArray.append(client)

            }
        }

        prepareData()

        DispatchQueue.main.async {
            self.tableView.reloadData()
        }

    }
}

函数prepareData()对数组进行排序并获取索引(A,B,C ...)

//sorts and makes the index
func prepareData() {
    let firstLetters = self.tableArray.map { $0.nameFirstLetter }
    let uniqueFirstLetters = Array(Set(firstLetters))

    self.sortedFirstLetters = uniqueFirstLetters.sorted()
    self.sections = self.sortedFirstLetters.map { firstLetter in
        return self.tableArray
            .filter { $0.nameFirstLetter == firstLetter }
            .sorted { $0.name < $1.name }
    }
}

beginUpdates()endUpdates()之间执行删除操作。

例如 :

tableView.beginUpdates()
tableView.deleteRows(at: [selectedIndexPath], with: .automatic)
tableView.endUpdates()

确保在使用deleteRowsinsertRows时更新dataSource

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM