从具有多个部分的表视图中删除
Deleting from a tableview with multiple sections
我正在尝试删除包含多个部分的 UITableView
中的一行。当有 1 个部分时,我看到的所有教程都会删除一行。
我的数据 [[String]]:
[["Section 0, Item 0", "Section 0, Item 1"], ["Section 1, Item 0"]]
无论我滑动哪一行,我都会收到错误消息:
'Invalid update: invalid number of sections. The number of sections contained in the table view after the update (1) must be equal to the number of sections contained in the table view before the update (2), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).'
我的删除代码:
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let pulled = UIContextualAction(style: .destructive, title: "Pulled", handler: {(_, _, completionHandler) in
self.pullList.remove(at: indexPath.row)
self.tableView.deleteRows(at: [indexPath], with: .fade)
completionHandler(true)
})
return UISwipeActionsConfiguration(actions: [pulled])
}
如果有专门针对此的教程那就太棒了。我的部分可能有 1-4 行。
如果 pullList
是 [[String]]
你必须删除
self.pullList[indexPath.section].remove(at: indexPath.row)
我正在尝试删除包含多个部分的 UITableView
中的一行。当有 1 个部分时,我看到的所有教程都会删除一行。
我的数据 [[String]]:
[["Section 0, Item 0", "Section 0, Item 1"], ["Section 1, Item 0"]]
无论我滑动哪一行,我都会收到错误消息:
'Invalid update: invalid number of sections. The number of sections contained in the table view after the update (1) must be equal to the number of sections contained in the table view before the update (2), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).'
我的删除代码:
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let pulled = UIContextualAction(style: .destructive, title: "Pulled", handler: {(_, _, completionHandler) in
self.pullList.remove(at: indexPath.row)
self.tableView.deleteRows(at: [indexPath], with: .fade)
completionHandler(true)
})
return UISwipeActionsConfiguration(actions: [pulled])
}
如果有专门针对此的教程那就太棒了。我的部分可能有 1-4 行。
如果 pullList
是 [[String]]
你必须删除
self.pullList[indexPath.section].remove(at: indexPath.row)