0

我想在分组表视图的标题中显示三个字符串。问题是我附加了这个字符串tableView titleForHeaderInSection,我想用不同的颜色和字体在不同的行中显示每个字符串。

我目前拥有的是:

在此处输入图像描述

我想要的是:

在此处输入图像描述

我得到并附加这三个字符串如下

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger) section 
{

    NSString * presenter = [[self agenda] getBriefingPresenter:section];
    NSString * time = [[self agenda] getBriefingTime:section];
    NSString * subject = [[[[self agenda] getMeetingBriefings] objectAtIndex:section] objectForKey:@"subject"];

    return [NSString stringWithFormat:@"%@\n%@  %@", subject, time, presenter ];

}

处理页眉字体的方法

- (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];
    label.frame = CGRectMake(45, 0, 484, 23);
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont fontWithName:@"Century Gothic" size:17];
    label.text = sectionTitle;
    label.lineBreakMode = UILineBreakModeWordWrap;
    label.numberOfLines = 2;
    [label sizeToFit];
    label.backgroundColor = [UIColor clearColor];

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

    return view;
}

那么如何解析这个 sectionTitle并在三行中打印为三个不同的标签呢?

4

1 回答 1

2

(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section中,创建 3 个具有特定字体和颜色的不同标签。您不能在单个 UILabel 上设置不同的字体/颜色;

所以你的代码更新了:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    NSString * presenter = [[self agenda] getBriefingPresenter:section];
    NSString * time = [[self agenda] getBriefingTime:section];
    NSString * subject = [[[[self agenda] getMeetingBriefings] objectAtIndex:section] objectForKey:@"subject"];

    // Create label with section title
    UILabel *presenterLabel = [[UILabel alloc] initWithFrame:CGRectMake(45, 0, 484, 23)];
    presenterLabel.textColor = presenterColor;
    presenterLabel.font = presenterFont;
    presenterLabel.text = presenter;
    presenterLabel.backgroundColor = [UIColor clearColor];
    [presenterLabel sizeToFit];

    UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(45, presenterLabel.frame.size.height, 484, 23)];
    timeLabel.textColor = timeColor;
    timeLabel.font = timeFont;
    timeLabel.text = time;
    timeLabel.backgroundColor = [UIColor clearColor];
    [timeLabel sizeToFit];

    UILabel *subjectLabel = [[UILabel alloc] initWithFrame:CGRectMake(45, timeLabel.frame.origin.y + timeLabel.frame.size.height, 484, 23)];
    subjectLabel.textColor = subjectColor;
    subjectLabel.font = subjectFont;
    subjectLabel.text = subject;
    subjectLabel.backgroundColor = [UIColor clearColor];
    [subjectLabel sizeToFit];


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

    return view;
}

你不需要- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger) section

于 2012-09-18T22:14:46.703 回答