Set UILabel text from UITableView header section

sjors picture sjors · Jan 23, 2013 · Viewed 12.8k times · Source

I've got an sectioned UITableView and want to set a label (not in the tableview) the text of the label that's on top.

I tried settings label with [label setText:@"name"]; in

 (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 

but that didn't work. Anyone else have ideas how to do this?

Answer

Radu picture Radu · Jan 23, 2013

Try this

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 60)];
    headerView.backgroundColor = [UIColor clearColor];

    UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(0,0, 50, 50)];
    label.backgroundColor = [UIColor yellowColor];
    label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;

    [headerView addSubview:label];
    return headerView;
}