Samuel
Samuel

Reputation: 75

Setting sectioned table header using nsfetchedresults controller

I am using NSFetchedResultsController in my project along with core data. I am able to fetch all the objects from the database. I am displaying them in a sectioned table view. I want to display the total number of rows in each section as the header of the section table view. I am not getting any kind of help for this. I have tried a lot but don't know how exactly to set it . Any kind of help is appreciable.

Upvotes: 0

Views: 89

Answers (1)

Dan Fairaizl
Dan Fairaizl

Reputation: 2170

Something like the following should work:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    NSInteger numberOfRows = [[[self.frc sections] objectAtIndex:section] numberOfObjects];

    UITableViewHeaderFooterView *sectionHeaderView = [[UITableViewHeaderFooterView alloc] init];
    sectionHeaderView.textLabel.text = [NSString stringWithFormat:@"Total objects %d", numberOfRows];

    return sectionHeaderView;
}

The trick is getting the right section out of the NSFetchedResultsController and calling the numberOfObjects method on it. The sections in an NSFetchedResultsController are proxy objects that conform to the NSFetchedResultsSectionInfo delegate.

Upvotes: 1

Related Questions