Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 793
Phone Guy Avatar asked Jun 03 '09 02:06

Phone Guy


2 Answers

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;
}
like image 132
Kristopher Johnson Avatar answered Nov 04 '22 06:11

Kristopher Johnson


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 !

like image 21
prakash Avatar answered Nov 04 '22 05:11

prakash