views:

24

answers:

1

Hi

I would like for my section header to display the sum of the values in my rows. I therefore implement the:

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

delegate function and place a UILabel in the custom view I build here.

If this were a UITableViewCell I would build the cell if it did not exist, then update it, or if it exists - only update it.

I don't know which "pattern" to use to update my Section Header. Either there is a "right way" build into the UITableView, but I can't seem to find an "updateSectionHeaderForSection" and call this only when I change the value of a row.

What puzzles me is how the UITableView deals with headers, does it call viewForHeadersInSection only once on reloadData/instantiation or does it call it all the time, i.e. does it instantiate the view repeatedly when scrolling if I place this code in the viewController?:

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

        UIImageView *shadow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"topbar.png"]];

}

And how do I force the section header to update the view, like calling updateRowsAtIndexPaths, when I have changed the value it should display?

I can't seem to find Apples take on this in the UITableView Programming Guide.

Thanks in advance for any help given.

A: 

I had to do a similar thing recently, the way I did it was to draw a custom view in viewForHeaderInSection. This view contained a number of different components and for the label that I wanted to show the total in I gave it a tag

detailLabel.tag=100001;

Then, whenever I wanted to update the total I retrieve the associated view

UILabel *total = (UILabel*)[self.view viewWithTag:100001];

And update it to the value I wanted:

total.text = @"1234";
Liam
Hi Liam and thanks. I found the delegate methods that gets called following a [tableView endUpdates], one of them is tableView:viewForHeaderInSection: luckily the code getting executed when I need to update the total is incased in begin/endUpdates so in there I did as you suggest above:)
RickiG