views:

192

answers:

1

It's been a while since I messed with my TableViewController so I'm a bit rusty...but now I'd like to set it up so that when a section has zero rows to display (array is empty) then it will "lie" to the controller and return 1 for numberOfRowsInSection. Then in the cellForRowAtIndexPath it will place a UILabel over the one row's cell that says something to the effect of "this section is empty."

Problem is when the table is in editing mode and user deletes the last row I get the following error:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 1. 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 (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted).

It seems to be upset that after the swipe-to-delete a row when it tries to redraw the table it finds that last row is still there. Where/how should I be handling this? Certainly I'm crapping all over Apple's HIG to some extent but it's confusing to my users to have just a header over an empty section...

thanks!

A: 

You need to actually insert your "fake" row using -insertRowsAtIndexPaths:withRowAnimation:. From UITableView's point of view, there are no "real" rows or "fake" rows. There are whatever rows the datasource says there are. So you had 1 row, and then you deleted 1 row, and then UITableView asked you "how many rows are there" and you said 1, and UITableView said "1 - 1 = 1. Wait, what? Blam."

If you want to insert a row ("real" or "fake" is all the same), you need to tell UITableView you're inserting it.

Rob Napier