views:

37

answers:

2

Hey all,

I'm using custom headers for my tableview...

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
   CustomHeaderController *header = [[CustomHeaderController alloc]
    initWithNibName:@"TableHeader" bundle:nil];

  header.title.text = @"Test";

  return header.view;
}

The title label is never set though. I even tried creating a viewWillAppear method and setting it there, but that didn't work. My outlets are set up too!

Thanks!

SOLUTION: View wasn't load on the return header.view call. Call header.view or add a viewDidLoad method to the class of header to get it to work! Thanks all!

+1  A: 

The function expects a UIView to be returned, but you appear to be returning a UIViewController. Did you mean to do return header.view?

Brian
Oops! Made my code simpler and forgot to include .view. I'm definitely doing that. Will edit my question. Sorry!
mark
+3  A: 

So I'm not sure "title" here means what you think it means. Setting the title on a view controller isn't going to affect what's in the view. If you're trying to set a title on a section, you may just want to use tableView:titleForHeaderInSection. tableView:viewForHeaderInSection is for more complex section headers (like if you wanted to put buttons, or multiple rows of text or something like that). If it matters, you can use both of these methods. I'm not 100% certain on the order they're called, but I'm pretty sure it looks for a viewForHeaderInSection and then if that's nil it goes to titleForHeaderInSection.

Dave Klotz
Hey Dave - I was going for a custom view that I was loading, not the stock Apple ones. Turns out the view doesn't load itself until it's shown so you have to force the view to appear and set it there by either calling header.view or adding a - (void) viewDidLoad method and setting labels in there!
mark
Someone needs to get answer credit. It's all you.
mark
Hmm...OK, well thanks for vote!
Dave Klotz