UITableView部分标题全黑

时间:2009-06-03 02:41:33

标签: iphone uitableview

对于iPhone,我有一个UITableView被分组,有一个部分,我在其中设置了一个节标题,它是来自笔尖的UILabel对象。显示表格视图时,标题显示为纯黑色条纹 - 无文本。

在heightForHeaderInSection中,我将高度设置为UILabel对象的frame.size.height。当我在IB中改变高度时,黑条纹的高度会发生变化。所以我知道.m文件已经锁定到正确的UILabel对象。

在调试器中,在viewForHeaderInSection中,似乎UILabel对象的宽度为零,高度为1079574528,文本为空。

对我做错了什么的想法?

5 个答案:

答案 0 :(得分:25)

不确定你做错了什么,但这里有一些可能有用的示例代码(来自我博客上的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;
}

答案 1 :(得分:1)

我遇到了同样的问题,并且还没有想到为什么黑条吧..

但是,不是在委托方法中提供页眉和页脚视图, 如果我设置了tableView.tableHeaderViewtableView.tableFooterView的值,那就没关系了!

答案 2 :(得分:0)

您可以发布 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;
}

答案 3 :(得分:0)

3.1.3不喜欢[UIColor clearColor];尝试使用与tableview相同的背景颜色

答案 4 :(得分:0)

我在加载数据源后刷新时观察到相同的行为。 我注意到这是由于我刷新表视图的方式。

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