【发布时间】: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