This problem has kept me busy for the last hours. I have two sections with one row in each section. When I delete the row in one of the sections than it throws an exception saying this is an invalid update (number of rows/sections before and after the update are not the same). This is understandable as I delete the last row of a section and I therefore delete the section. The question is how to avoid the exception.
Everything is okay with my data source. I checked and rechecked (believe me).
So, as the title of the thread states, how do you delete the last row of a section without getting an exception?
Thanks,
Bart
When you delete a row, and this row is the last one of its section, you need to also delete the section. Basically, you need to keep track of both the indexPaths you want to delete, which are associated to the rows, and the indexes related to the sections that needs to be removed because they no longer contain rows. You could do it as follows:
Each time you delete an object from your model array related to a specific section of the tableView, check if the array count is zero, in which case add an index representing the section to indexes:
Determine all of the indexPaths related to the rows to be deleted, then update the tableView as follows:
This worked for me and other people I suggested the approach.
Perhaps this may work for you:
Hope this is what you were looking for.
This one should do the trick:
For Swift 2, after removing the data in your model: