勿绮语
勿绮语

Reputation: 9320

Customized table section header does not work

I created a customized section header with the following code, but the red label did not show.

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{
    UIView *headerView = [[[UIView alloc] init] autorelease];

    UILabel * label1 = [[UILabel alloc] init];
    label1.textColor = [UIColor redColor];

    [headerView addSubview:label1];

    return headerView;
}

Upvotes: 1

Views: 560

Answers (1)

Mark Adams
Mark Adams

Reputation: 30846

The designated initializer for UILabel is -initWithFrame: and not -init. Your label isn't appearing because you have not specified a frame for the view.

Additionally, I noticed that you haven't initialized headerView with a frame either, but that appears to be working because the UITableView is probably setting a new frame at some point. You should get in the habit of using the designated initializer.

UIView Class Reference

Upvotes: 1

Related Questions