0

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).'"

2
  • 2
    First remove the item from the data source array at given index, then call deleteRows Commented Feb 27, 2020 at 12:08
  • I tried it works thank you Commented Feb 27, 2020 at 12:15

1 Answer 1

3

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).'

It says the exact thing you gotta do. Remember when deleting data, you need to make your DATASOURCE count equal to the rows and section count after the method deleteRows or deleteSections.

Which means you manipulate your data source array before calling those methods. And when using multi-sections, remember to carefully access your datasource by both section and row, and also when deleting and inserting data.

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {

        let contextItem = UIContextualAction(style: .destructive, title: "Delete") {  (contextualAction, view, boolValue) in

            let section = indexPath.section
            let row = indexPath.row

            self.data[section].remove(at: row)
            self.tableView.deleteRows(at: [indexPath], with: .automatic)

            boolValue(true)
        }

        let swipeActions = UISwipeActionsConfiguration(actions: [contextItem])


        return swipeActions
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.