UITableView Header Header - Black

For iPhone, I have a UITableView that is grouped, has one section, and in which I set up the section title, which is a UILabel object from nib. When a table view is displayed, the title is displayed as a solid black bar - no text.

In heightForHeaderInSection, I set the height as frame.size.height of a UILabel object. When I change the height in IB, the height of the black bar changes. Therefore, I know that the .m file is fixed to the right of the UILabel object.

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

Any thoughts on what I'm doing wrong?

+5
source share
5

, , , ( post ):

#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;
}
+25

, , .

, , tableView.tableHeaderView tableView.tableFooterView, !

+1

heightForHeaderInSection viewForHeaderInSection? , , , , , ...

, IB - . viewForHeaderInSection, :

- (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;
}
0

3.1.3 [UIColor clearColor]; , tableview

0

. , , .

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

All Articles