Displaying the message "No rows found" in a UITableView with master data

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.

+7
source share
4 answers

Instead of hacking a tableview data source to get the intended user interface, you should add a “No Rows” message to the table header.

+12
source

In viewDidLoad, I did the following.

 UILabel *label = [[UILabel alloc] init]; [label setTextColor:[UIColor lightGrayColor]]; [label setText:@"No widgets found."]; [label sizeToFit]; label.frame = CGRectMake((self.tableView.bounds.size.width - label.bounds.size.width) / 2.0f, (self.tableView.rowHeight - label.bounds.size.height) / 2.0f, label.bounds.size.width, label.bounds.size.height); [self.tableView insertSubview:label atIndex:0]; 

In this case, each TableViewCells must be opaque to hide the label. or you need to switch the hidden label property according to the number of lines.

+7
source

My simple suggestion to show a blank message is to rearrange your controller as a simple UIViewController (and not a UITableViewController ).

This UIViewController consists of a UITableView (the controller is the data source and delegate for your table) and a UILabel (or UIView that contains the UILabel ) that displays an empty row message.

This way you can control the visibility of the table and label based on the extracted rows.

This approach can be time consuming, but I think it's good to avoid hacking the NSFetchResultsController and data source. In addition, you can have full control over the placement of the position for your empty message.

As @Rog shows, you can also use the table header to display this message. As you prefer.

Hope this helps.

+1
source

An alternative approach that I used earlier is to use Core Data to control the update for you by inserting a "no rows" object for the section in which none of the rows were found in your model class that handles updating the data.

There are several ways to implement this, for example. set the name / title field to a known status message or flag inside the object. After insertion, you can find the "no rows" entity in the cellForRowAtIndexPath delegate method and insert an alternative table cell to display the message.

Just delete the "no rows" object before updating the data for this section.

+1
source

All Articles