1

I've got a An array which is a an array of Sections in the Table View. I have got another array that contain arrays in it. I displayed it on the table view correctly, but each time I want to delete it, I get an error. I want to delete the exactly item in the array which is inside the array. Here is what I got so far:

        class ViewController: UIViewController, UITableViewDelegate {

        @IBOutlet var tableView: UITableView!
        var eachSection = ["a","ss","dd","heyyyyyy","3"]
        var eachStep = [["z","zz"],["x","xx"],["c","cc","ccc"],["r","rr","rrr","rrrr"],["o"]]

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

        func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            let sectionString = Array(eachStep)[section]

            return sectionString.count
        }

        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)

            // Configure the cell...
            let sectionString = Array(eachStep)[indexPath.section]
            cell.textLabel?.text = sectionString[indexPath.row]
            print(sectionString)
            return cell
        }

        func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
            let sectionString = eachSection[section]
            return sectionString
        }

        func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
            let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
            if (editingStyle == UITableViewCellEditingStyle.Delete) {
                var sectionString = Array(eachStep)[indexPath.section]
                sectionString.removeAtIndex(indexPath.row)
                tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
            }
        }
}

This is the error I got:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), 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)

3
  • is your array is mutable ? and are the data in the array from websrvice response(json formated data) ? Commented Mar 25, 2016 at 11:48
  • @OlegGordiichuk Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), 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). Commented Mar 25, 2016 at 11:50
  • Hello. Please don't post error messages or code in the comments (it's not readable and comments can be deleted at any time). You should edit your question instead. Thank you! Commented Mar 25, 2016 at 11:54

1 Answer 1

2

This error contains answer for youre question.

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), 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).

Look carefully what are you doing.

You delete items from one array that you are not using as data source.

 var sectionString = Array(eachStep)[indexPath.section]
 sectionString.removeAtIndex(indexPath.row)

Try this:

eachStep[indexPath.section].removeAtIndex(indexPath.row)
Sign up to request clarification or add additional context in comments.

3 Comments

Yea, this is the error, But how I correct it and why? I understand my mistake now but I don't know how to correct it
sectionString is a copy of the array inside eachStep. You need to do something like eachStep[indexPath.section] = sectionString to copy it back.
Your datasource was always full because you delete items from the copy of your datasource.

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.