Phone Guy
Phone Guy

Reputation: 283

UITableView section header is all black

For an iPhone, I've got a UITableView that is grouped, has one section, and in which I've set up a section header that's a UILabel object from the nib. When the table view displays, the header shows up as a stripe of solid black -- no text.

In heightForHeaderInSection I've set the height to be the frame.size.height of the UILabel object. When I change the height in IB, the black stripe's height changes. So I know that the .m file has latched on to the right UILabel object.

In the debugger, in viewForHeaderInSection, it seems that the width of the UILabel object is zero, and the height is 1079574528, and the text is null.

Any thoughts on what I'm doing wrong?

Upvotes: 5

Views: 24823

Answers (5)

dotnetcoder
dotnetcoder

Reputation: 3601

I observed same behavior when refreshing after datasource is loaded. I noticed this was due to the way i was refreshing table view.

//[self loadView];   this caused the section header to go black.
[self.tableView reloadData]; // this works! 

Upvotes: 0

Todd Horst
Todd Horst

Reputation: 863

3.1.3 doesnt like [UIColor clearColor]; try using the same background color as your tableview

Upvotes: 0

prakash
prakash

Reputation: 59679

I had the same issue and haven't quite figured why the black bar..

BUT, instead of providing the header and footer views in delegate methods, if i set values for tableView.tableHeaderView and tableView.tableFooterView, its all fine !

Upvotes: 1

Kristopher Johnson
Kristopher Johnson

Reputation: 82535

Not sure what you're doing wrong, but here is some example code that might help (from a post on my blog):

#define SectionHeaderHeight 40


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if ([self tableView:tableView titleForHeaderInSection:section] != nil) {
        return SectionHeaderHeight;
    }
    else {
        // If no section header title, no section header needed
        return 0;
    }
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
    if (sectionTitle == nil) {
        return nil;
    }

    // Create label with section title
    UILabel *label = [[[UILabel alloc] init] autorelease];
    label.frame = CGRectMake(20, 6, 300, 30);
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor colorWithHue:(136.0/360.0)  // Slightly bluish green
                                 saturation:1.0
                                 brightness:0.60
                                      alpha:1.0];
    label.shadowColor = [UIColor whiteColor];
    label.shadowOffset = CGSizeMake(0.0, 1.0);
    label.font = [UIFont boldSystemFontOfSize:16];
    label.text = sectionTitle;

    // Create header view and add label as a subview
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, SectionHeaderHeight)];
    [view autorelease];
    [view addSubview:label];

    return view;
}

Upvotes: 25

Dutchie432
Dutchie432

Reputation: 29160

Can you post the code for your heightForHeaderInSection and your viewForHeaderInSection functions? The theory behind what you're doing sounds correct, but without seeing the code, it would be nearly impossible to figure out the issue...

It sounds like you place a label on the view in IB and are trying to use that as your header view - which is not the proper way of doing things. If you aren't using viewForHeaderInSection, then give that a try.. like this:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *lbl;
    lbl.text = @"Header for The Only Section";
    //define other properties for the label - font, shadow, highlight, etc...

    return lbl;
}

Upvotes: 0

Related Questions