Petar
Petar

Reputation: 45

Cell lags after tapping delete in swift

So I made a tableview which is populated with CoreData objects. The cell itself has a custom class for the title and two other buttons. But the delete method is the classic apple type:

 func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]?

Anyhow, all the cells are loaded nicely, I can sort them, change their values etc., BUT, when I tap delete after the swipe to left, the cell remains fixed on the swiped position and I get this error on the console:

"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 (8) must be equal to the number of rows contained in that section before the update (8), 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). with userInfo (null) "

After that, I cannot do anything with that cell. Interesting though, the cell itself and the CoreData object are deleted, because if I change the view from the table view controller to my main view, and again to the table view, the cell is no longer there. At first I thought I just need to call the

     tableView.reloadData()     

method but it didn't work. Note that this does not cause the crash, but if I retap the delete button again, no doubt it will crash since the object has been successfully removed.

Any ideas here?

Upvotes: 1

Views: 242

Answers (1)

Marcus S. Zarra
Marcus S. Zarra

Reputation: 46718

The UI is separate from Core Data. Core Data is the persistence layer and the data you wish to display gets mirrored in the UI.

You are deleting the data in Core Data but you are not updating the UI correctly.

If you are using a NSFetchedResultsController then you have an error in the delegate methods. Use the debugger and breakpoints to find out why you are not correctly removing the row from the UI.

If you are not using a NSFetchedResultsController then your notification of the table view of the deletion is incorrect.

Upvotes: 1

Related Questions