ios - Swift - 在 tableView.deleteRows 上崩溃(在 : [selectedIndexPath], 处,: . 自动)

标签 ios swift tableview

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

调试器显示该行上的错误:tableView.deleteRows(at: [selectedIndexPath], with: .automatic)

通过此消息: *2018-02-05 23:13:42.355696-0800 发票[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()

关于ios - Swift - 在 tableView.deleteRows 上崩溃(在 : [selectedIndexPath], 处,: . 自动),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48637600/

相关文章:

ios - UIViewAnimationTransitionCurlUp 水平工作

ios - UIDeviceOrientationDidChangeNotification 返回错误的方向

swift - 在 Swift 中使 Realm Loop 通用且可重用

ios - 以编程方式在 Swift 中添加联系人

javafx - 将 CheckBox 列添加到现有 TableView

JavaFX TableView 列宽\内容自动截断

ios - 如何查找我的 mac 中是否安装了 cocoaPods

ios - 有没有办法检查 iOS 设备是否已锁定/解锁?

swift - 在 Wunderground API 中访问一个值

ios - UITableView 滚动时复选标记消失