0

I have created two buttons (add and edit):

var addButton = UIBarButtonItem(title: "Add", style: .Plain, target: self, action: "tabBarAddClicked"); self.navigationItem.rightBarButtonItems = [self.editButtonItem(), addButton]

These also work and I can delete objects from the tableView with the function

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

Hereby:

var subjects: [Subject] = subjectsData

and

var subjectsData = [ Subject(name: "Investments", semester: 1), Subject(name: "Statistics", semester: 1), Subject(name: "Studium Universale", semester: 2) ] 

Though, when I go back in the navigation bar to the previous screen and then return to the screen, the deleted objects from the array are again there. But I told it to remove the objects from the array subjectsData with subjects.removeAtIndex(indexPath.row).

Why do the objects return and how can I change that?

1 Answer 1

1

But I told it to remove the objects from the array subjectsData with subjects.removeAtIndex(indexPath.row).

You told it to remove from subjects, not subjectsData.

For example:

var array1 = [1, 2, 3]
var array2 = array1


array2.removeFirst()

print(array1) // [1, 2, 3]
print(array2) // [2, 3]
Sign up to request clarification or add additional context in comments.

2 Comments

But is subjects not subjectsDatabecause of var subjects: [Subject] = subjectsData?
If I change the code to subjectsData.removeAtIndex(indexPath.row), the app crashes with the error: "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 (4) must be equal to the number of rows contained in that section before the update (4), 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).'"

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.