1

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

Edelmundo
  • 23
  • 1
  • 4
  • What was your problem with `reloadSections:withRowAnimation`? – Jörn Eyrich Aug 17 '12 at 16:35
  • My issue is that in mytableviewcell i have a UISlider, what i want to happen is when i change the slider value that the sum in the header is changed accordingly to reflect the change of the slider value change. However because i call my configurecell event when a cell value changes the the reload sections is called multiple times when the slider value changes so the table view will go mental any ideas ? – Edelmundo Aug 21 '12 at 07:59
  • oh, I see. If you don't need to update the header **while** you are sliding but only **after** you have moved the slider to a new value, you can set the slider to be non-continuous (`mySlider.continuous=NO;`). Otherwise you should go with Fabian's solution. – Jörn Eyrich Aug 21 '12 at 15:58
  • i'm experiencing exactly this in ios8 - I have a header section and it just doesn't update until I scroll the whole thing out of view What wast he problem for you and how did you fix it? – NullHypothesis Aug 24 '14 at 21:31

1 Answers1

3

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.

Fabian Kreiser
  • 8,307
  • 1
  • 34
  • 60
  • Hi Fabian, from your example i assume i would have to create a separate label for each section header ? i currently have approx 50 sections in my data and this will be constantly changing as more data is added to my app, could you elaborate on the use of an array to store the labels so that i can then access them individually ? – Edelmundo Aug 21 '12 at 13:48
  • UITableView takes care about releasing the labels when they're not needed anylonger, don't worry too much about performance issues. A future version of the iOS might even support reusable table header footer views. If you need to access the labels, you'll have to store them in an array, because there's no method on UITableView to get the views used for the headers back. You should make sure to somehow use weak references to the labels, because otherwise the labels won't be released. See this question: http://stackoverflow.com/questions/9336288/nsarray-of-weak-references-to-objects-under-arc – Fabian Kreiser Aug 22 '12 at 06:42