1

The error: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1), 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).'

Looking at many related posts, I still cannot find the answer.

Here are the relevant code snippets (does not contain all of the code):

var todoItems : [Assignment] = []
var sections = Dictionary<String, Array<Assignment>>()
var sortedSections = [String]()

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == UITableViewCellEditingStyle.Delete {
        var tableSection = sections[sortedSections[indexPath.section]]
        tableSection!.removeAtIndex(indexPath.row)
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
    }

}


override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return sortedSections[section]
}

override func viewDidLoad() {
    super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return sections.count
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return sections[sortedSections[section]]!.count
}

Thanks for the help!

1 Answer 1

1

You deleted element in array but update of Dictionary with sections is missed :(

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == UITableViewCellEditingStyle.Delete {
        var tableSection = sections[sortedSections[indexPath.section]]
        tableSection!.removeAtIndex(indexPath.row)

        //add this line
        sections[sortedSections[indexPath.section]] = tableSection

        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
}

}

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

4 Comments

How would I go about deleting the section as well, if the item was the only one in the section?
Content of cells in your table are defined here var sections = Dictionary<String, Array<Assignment>>() , but you delete row only tableSection (var tableSection = tableSection[sortedSections[indexPath.section]]) but Dictionary tableSection was not updated <- this is the resaon of error
I also have Sections (headers) above each cell. However, if I delete only the cell, the Section Header stays. I am wondering how to delete that as well?
You have to add check if section has any cell. If there is no any cell in section - you have to remove sectino too.

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.