Programmatically edit content in the header view



Instead of adding buttons to the header view I tried to add a text label. When I tried to edit its contents from view did load nothing changed.

I tried

label.text=@"This will not be displayed" in viewdidload

This did not work.

The solution I came up with was to edit the content from the

function and i am finding this has its limitations especially when getting the data to set the label from the internet.

IS there a standard way to edit the content of the second loaded xib programatically?



I’m afraid I’m having a little trouble understanding exactly what was tried.

We didn’t have a field called label, so I assume you declared it in the header and connected it to the field in the nib via File’s Owner.

-viewDidLoad isn’t called every time an arbitrary view is loaded; it’s called specifically by the UIViewController whenever the view pointed to by its view property is loaded.

To change the label text at run time, just go to where you loaded the nib and add the statement there:

- (UIView *)headerView 
    // If we haven't loaded the headerView yet...
    if (!headerView) {
        // Load HeaderView.xib
        [[NSBundle mainBundle] loadNibNamed:@"HeaderView" owner:self options:nil];
        label.text = @"hello, world";  //or the preferred [label setText: @"hello, world"];
    return headerView;


Hi GC3182,

Thanks for the fast response!

Sorry i didn’t make myself clear. You are correct in your assumptions.

I have tried putting the statement in (UIView *)headerView there and it simply does not work…

The only way i can get it to work is to put it in

What I am trying to do is to populate the subview with information from xml which I am parsing. The parser does not finish before the

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section function is called and so the

the label is not refreshed with the actual text until i flick the sub view off the screen. (presumably i call -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section when this happens and that is why it then shows the text label populated with some of the parsed xml data)…

Do you have any suggestions?


Oh my word. Well that kind of changes things a little.

When you say you “put the statement in -headerView” (as in the code snippet I included), the statement you put there was not setting the label to a constant string but to a field that came from parsing the XML? So by “simply does not work” you mean the label text (if it started with any) was cleared?

Just guessing, I’d start with trying [label setNeedsDisplay] after you’ve parsed the XML and set the label text.