如何在titleForHeaderInSection方法中更改字体样式和背景颜色

17

阅读和检查了长时间的代码后,我很自豪地拥有了一个自定义的带有节和节标题的表格视图,所有内容都来自核心数据对象。现在我需要自定义节标题和背景颜色。我看到过在viewForHeaderInSection方法中完成,但能否在我的titleForHeaderInSection方法中实现呢?以下是我的方法:

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




    id <NSFetchedResultsSectionInfo> theSection = [[self.fetchedResultsController sections]objectAtIndex:section];
    NSString *sectionname = [theSection name];
    if ([sectionname isEqualToString:@"text 1"]){
        return @"Today";

    }
    else if ([sectionname isEqualToString:@"text 2"]){
        return @"Tomorrow";
    }


    if ([[self.fetchedResultsController sections]count]>0){
        id<NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections]objectAtIndex:section];
        return [sectionInfo name];
    }
    else{
        return nil;
    }

}
5个回答

26

简单优化

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    // Background color
    view.tintColor = [UIColor whiteColor];//[UIColor colorWithRed:77.0/255.0 green:162.0/255.0 blue:217.0/255.0 alpha:1.0];
    // Text Color
    UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
    [header.textLabel setTextColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"tableSection"]]];

}

3
聪明,但值得注意的是,由于API不能保证view是一个UITableViewHeaderFooterView,所以在将来的iOS版本中可能会崩溃。为了更安全,建议使用条件转换。 - Aaron Brager

13
这是一个使用现有代码设置标题文本的示例,但它允许您使用UITableViewHeaderFooterView来调整外观:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    static NSString *header = @"customHeader";

    UITableViewHeaderFooterView *vHeader;

    vHeader = [tableView dequeueReusableHeaderFooterViewWithIdentifier:header];

    if (!vHeader) {
        vHeader = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:header];
        vHeader.textLabel.backgroundColor = [UIColor redColor];
    }

    vHeader.textLabel.text = [self tableView:tableView titleForHeaderInSection:section];

    return vHeader;
}

如果您想要,甚至可以像子类化UITableViewCell一样,子类化UITableViewHeaderFooterView来进一步自定义外观。


可以,谢谢。我能为每个部分标题选择不同的背景颜色吗? - mvasco
当然!任何改变的内容都应该在if (!vHeader)条件范围之外。任何始终相同的内容都应该在其中。(就像表格单元格一样。) - Aaron Brager
谢谢,我已经根据部分值更改了背景颜色,并且它有效,但现在我需要在标题旁边添加部分中的行数作为标签,例如TODAY(6),TOMORROW(17)...我能否在此方法内实现这个功能? - mvasco
这已经被弃用,因为你使用的是iOS 9。 - Ankish Jain
1
@AnkishJain,“dequeueReusableHeaderFooterViewWithIdentifier:”在文档中似乎并未被弃用。你指的是什么? - Aaron Brager

11

Swift实现@codercat的答案:

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {

    view.tintColor = UIColor(red: 0.967, green: 0.985, blue: 0.998, alpha: 1) // this example is a light blue, but of course it also works with UIColor.lightGray

    var header : UITableViewHeaderFooterView = view as UITableViewHeaderFooterView
    header.textLabel.textColor = .red

}

tableView(_:viewForHeaderInSection:)难道不是更好的解决方案吗? - ShadeToD

1

通过以下方式创建自定义HeaderView:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *viewHeader = [UIView.alloc initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 28)];
    UILabel *lblTitle =[UILabel.alloc initWithFrame:CGRectMake(6, 3, 136, 21)]; 
    [lblTitle setFont:[UIFont fontWithName:@"HelveticaNeue" size:13]]; //Font style 
    [lblTitle setTextColor:[UIColor blackColor]]; 
    [lblTitle setTextAlignment:NSTextAlignmentLeft]; 
    [lblTitle setBackgroundColor:[UIColor clearColor]]; //Background
    [viewHeader addSubview:lblTitle];
    return viewHeader;
}

将此标签赋予任何文本; 我建议为标题创建一个单独的NSArray。

谢谢,但是我需要titleForHeaderInSection方法来保留当前的方法来创建节行和标题... - mvasco
为什么特别是 viewForHeaderInSection 方法,它可以解决您的问题:id <NSFetchedResultsSectionInfo> theSection = [[self.fetchedResultsController sections]objectAtIndex:section]; 通过这个获取结果数组,获取任何值并设置标签文本。 - Zeeshan
你的意思是我可以在viewForHeaderInSection方法中使用与titleForHeaderInSection相同的代码行,然后添加你的建议来格式化文本和背景颜色? - mvasco
仅使用它从NSFetchResultsSection获取提取结果。然后将标签标题设置为它。简单易行。 - Zeeshan

0

我认为在titleForHeaderInSection中,您无法更改部分视图属性。 将viewForHeaderInSection添加到您的项目中,然后添加带有标签的自定义视图,例如 请参阅此提示:

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0)] autorelease];

    UILabel *label = [[[UILabel alloc] init] autorelease];
    label.frame = CGRectMake(0, 0,300, TABLE_HEADER_HEIGHT);
    label.backgroundColor = [UIColor clearColor];
    label.textColor = TABLE_HEADER_TITLE_COLOR;
    label.font = [UIFont fontWithName:FONT_NAME size:14.f];

    label.text = [self tableView:tableView titleForHeaderInSection:section];

    if (section == 3) {
        [headerView setBackgroundColor:[UIColor whiteColor]];
    } else {
        [headerView setBackgroundColor:[UIColor redColor]];
    }

    [headerView addSubview:label];

    return headerView;
}

您可以根据章节编号为标签设置标题。


你不应该在这里使用UIView,它不可重用。 - Aaron Brager
在我看来,我的代码一切都很好,UIView 不应该被重复使用。 - Robert
什么样的错误?你不应该逐字复制那段代码,这只是给你自定义的提示,例如TABLE_HEADER_HEIGHT和TABLE_HEADER_TITLE_COLOR是我定义的值。 - Robert

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接