UITableView部分标题全黑

Pho*_*Guy 5 iphone uitableview

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

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

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

对我做错了什么的想法?

Kri*_*son 25

不确定你做错了什么,但这里有一些可能有用的示例代码(来自我博客上的帖子):

#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;
}
Run Code Online (Sandbox Code Playgroud)


Dut*_*432 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;
}
Run Code Online (Sandbox Code Playgroud)

  • 那是行不通的,因为你还没有初始化标签。您需要`[[UILabel alloc] initWithFrame:someFrame]`。然后,您可以设置其“text”属性。 (2认同)