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
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:
2) Update the label directly by changing its
text
property. You'll have to create aniVar
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
Good luck,
Fabian
Edit:
The code above assumes you're using ARC.