Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Introduction to Core Data with Swift 2 Deleting and Updating Data Animating Table View Cells

Davide Callegari
Davide Callegari
5,360 Points

I think there's something missing, I get an error if I try to delete an item (Invalid update: invalid number of rows)

CoreData: error: Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. 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 (2), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out). with userInfo (null)

2 Answers

Matthew Spear
Matthew Spear
14,534 Points

Solved it! - I ran into the same problem and realised what I had done wrong...

case.Delete:
      guard let indexPath = indexPath else { return }
      tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)

Check your code matches above, I made the mistake of trying to use newIndexPath (instead of indexPath) and putting the incorrect method call to the tableView (3 different methods for insert, delete and reload).

Hope that helps, Matt

Davide Callegari
Davide Callegari
5,360 Points

Haha, awesome, yes it worked! Damn you copy paste... Thanks for your help :)