I have implemented an iPhone application that uses UITableViewController / UITableView and Core Data. In addition, I use NSFetchedResultsController to manage table data. All this was very straightforward and works great. Then I decided that I should display the message in a UITableView if there are no rows where they are found / received. Having studied this, it turned out that the best way (perhaps the only way) to do this is to return the "dummy" cell containing this message. However, when I do this, I get nastygram from the runtime system, which complains (and rightfully) of data inconsistencies: "Invalid update: invalid number of partitions. Number of partitions contained in table view ...". Here is the relevant code:
- (NSInteger) numberOfSectionsInTableView: (UITableView *)tableView { if ([[self.fetchedResultsController fetchedObjects] count] == 0) return 1; return [[self.fetchedResultsController sections] count]; } - (NSInteger) tableView: (UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if ([[self.fetchedResultsController fetchedObjects] count] == 0) return 1; id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex: section]; return [sectionInfo numberOfObjects]; } - (UITableViewCell *) tableView: (UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath { if ([[self.fetchedResultsController fetchedObjects] count] == 0) { UITableViewCell *cell = [[UITableViewCell alloc] init]; cell.textLabel.text = @"No widgets found."; return cell; } STCellView *cell = (STCellView *)[tableView dequeueReusableCellWithIdentifier: @"ShieldCell"]; [self configureCell: cell atIndexPath: indexPath]; return cell; }
I read answers from similar questions and it seems that I should use
insertRowsAtIndexPaths: withRowAnimation:
to insert the message string "dummy" into my table. However, this also means deleting the "dummy" line when inserting a real line. I can do this, but there seems to be an easier way for this. All I want to do is show a message stating that there are no rows in the table (simple enough?). So my question is this: is there a way to display a message in a UITableView without using the “dummy” cell approach OR is there a way to convince the UITableViewController / NSFetchResulsController that it is just a “dummy” string and they shouldn't get so upset because it not a real row (from my point of view) in the table?
Any help you can provide will be greatly appreciated (I'm struggling for a newbie in iPhone development, and I want to learn best practices). Thanks.
user1092808
source share