尝试从表格 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/