views:

85

answers:

2

I have a UITableView tied to core data. When there are no entries, I hack the table to make sure that #sections = 1, #rows = 1, and that this row displays text that says "your table is empty; add a new item using the + button".

When the user adds his first item by clicking the + button, though, core data throws an exception saying that the number of rows after the update (1) is not equal to the number of rows before the update (1) plus or minus (1 inserted, 0 deleted). This makes sense, because Core Data presumably queries the same functions that my tableview does (numberOfSectionsInTableView and numberOfRowsInSection:section). But how do I tell core data that I was lying to the tableview, and that the number of rows before the update was actually 0?

A: 

Okay, I think I thought of a really hacky way to deal with this problem in the event that I did keep a single UITableView. Writing it down in case this is helpful in the future.

Basically, create a BOOL isUpdating to record whether we're currently in the middle of updating the table. The implementation numberOfRowsInSection:section should check whether self.isUpdating is true; if it is, then return the "lie" number of rows (i.e. the number that the NSFetchedResultsController expects the table to have), otherwise return the number of rows to tell the tableview. Then set isUpdating to true in controllerWillChangeContent and back to false in controllerDidChangeContent.

unsorted
A: 

It would help to show the code of how you are adding a row, maintaining state, etc. Otherwise any answer is only going to be very general or a guess.

Marcus S. Zarra