Mark
Mark

Reputation: 7576

iOS - setting outlets on a ViewController being used as a UITableView header

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!

Upvotes: 0

Views: 549

Answers (2)

Dave Klotz
Dave Klotz

Reputation: 376

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.

Upvotes: 3

Brian
Brian

Reputation: 15706

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

Upvotes: 1

Related Questions