How to handle an empty UITableView Section w/ Core Data and FetchedResultsController

Discussion in 'iOS Programming' started by mdhansen5, Jul 20, 2012.

  1. mdhansen5 macrumors member

    Joined:
    Nov 22, 2010
    Location:
    Colorado
    #1
    I'm using Core Data and an NSFetchedResults controller to populate a UITableViewCell (plain style) in my app. The TableView has 3 sections, each with a viewForHeaderInSection and a viewForFooterInSection. The user will be entering items that will be put in each section.

    When one of the sections has no items in it, it completely disappears. I'm wondering if it would be possible to make it so if a section is empty, it would display a new cell that would say "no entries" or something like that (maybe with a UIImageView or another view?), which would then go away if a new item was put into that section.

    Does anyone know how to accomplish this?

    Code:
    Data Source Code
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return [[self.fetchedResultsController sections] count];
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {    
        return [[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects];
    }
    
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
        return [[[self.fetchedResultsController sections] objectAtIndex:section] name];
    }
    Also, something to point out is that when I delete all the rows from the section, the entire section and it's header goes away as well, and doesn't just show an empty section.
     
  2. MattInOz macrumors 68030

    MattInOz

    Joined:
    Jan 19, 2006
    Location:
    Sydney
    #2
    Even using NSfetchedResultsController you can still put logic in-between the Tableview and fetchededResults.

    I'd start with number of rows for section makes sure it returns 1 if the section is empty. Then the fun bit you then need to make sure not to ask the FetchedResultsController for the item at index. Which will involve a few more of the Tableview methods.
     

Share This Page