Pergunta

Hi hope someone can help.

I currently have a tableview which has a set of sections, in my titleForHeaderInSection i am returning a string that includes a sum of values contained in the section cells to display in the section header. This is fine but when i update a cell value i want the titleForHeaderInSection to update and refresh my sum of values. At the moment the user needs to scroll the header out of sight then back in for it to refresh. I have been googling to see if i could find a solution seen a few examples that suggest including a label in the view for header but i need the sections to be dynamic so cant create labels for each section, i have also tried using the reloadsection but this doesent work properly either and the tableview reloaddata is to much of a performance hit to do each time a value changes in a tableview cell.

my current code for my titlerForHeaderInSection is

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];

int averageScoreTotal, _total;
averageScoreTotal = 0;
_total = 0;

for (BlkCon_BlockToConstructionType *sPC in sectionInfo.objects)
{
    _total = [sPC.compositionPc integerValue];

    averageScoreTotal += _total;
}   

return [NSString stringWithFormat: @"(Total Composition for Group %d)", averageScoreTotal];

}

Thanks in advance for any help

Foi útil?

Solução

You can use UITableView's -reloadSections:... method with the correct section. That will reload the section header, too.

If you don't want to use that method, because your table view stops scrolling for a moment or one of the table view cells is first responder, you have to use a custom header view for the section containing a label.

1) Implement -tableView:heightForHeaderInSection: and -tableView:viewForHeaderInSection:

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return tableView.sectionHeaderHeight;
}

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    CGFloat height = [self tableView:tableView heightForHeaderInSection:section];
    NSString *title = [self tableView:tableView titleForHeaderInSection:section];

    UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, height)];
    containerView.backgroundColor = tableView.backgroundColor;

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(19, 7, containerView.bounds.size.width - 38, 21)];
    label.backgroundColor = [UIColor clearColor];

    label.font = [UIFont boldSystemFontOfSize:17];
    label.shadowOffset = CGSizeMake(0, 1);
    label.shadowColor = [UIColor whiteColor];

    label.text = title;
    label.textColor = [UIColor colorWithRed:0.265 green:0.294 blue:0.367 alpha:1];

    [containerView addSubview:label];

    return containerView;
}

2) Update the label directly by changing its text property. You'll have to create an iVar for the labels or better use an array to store them, so you can access them when you want to update the section header's text.

3) If you want to make the header flexible in height, set the numberOfLines property of the label to 0 so that it has indefinite lines and make sure the -tableView:heightForHeaderInSection: returns the correct height.

In order to update the section header's height use

[self.tableView beginUpdates];
[self.tableView endUpdates];

Good luck,
Fabian

Edit:
The code above assumes you're using ARC.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top