I have a table view with two sections
I added the swipe to delete row
but the app crash cause there's error select the current indexPath
I tried two different ways but none of this works
//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
}
the result says: "Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 1. The number of rows contained in an existing section after the update (5) must be equal to the number of rows contained in that section before the update (5), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'"
deleteRows