【问题标题】:How to change font style and background color in titleForHeaderInSection method如何在 titleForHeaderInSection 方法中更改字体样式和背景颜色
【发布时间】:2014-02-03 03:45:54
【问题描述】:

经过长时间阅读和检查代码,我很自豪能够拥有一个包含部分和部分标题的自定义表格视图,所有这些都由核心数据对象填充。现在我需要自定义部分标题和背景颜色。我已经看到它完成了,但是使用了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;
    }

}

【问题讨论】:

    标签: ios uitableview


    【解决方案1】:

    简单优化

    - (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"]]];
    
    }
    

    【讨论】:

    • 聪明,但值得注意的是这可能会在未来的 iOS 版本中崩溃,因为 API 不保证 view 是 UITableViewHeaderFooterView。可能希望使用条件转换更安全。
    【解决方案2】:

    这是一个使用现有代码设置标题文本的示例,但允许您使用 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;
    }
    

    如果你愿意,你甚至可以继承 UITableViewHeaderFooterView 就像你继承 UITableViewCell 来进一步自定义外观。

    【讨论】:

    • 可以,谢谢,我可以在每个部分标题上选择不同的背景颜色吗?
    • 当然!任何更改都应超出 if (!vHeader) 条件的范围。任何始终相同的东西都应该在里面。 (就像表格单元格一样。)
    • 谢谢,我已经根据部分值更改了背景颜色,并且可以正常工作,但是现在我需要在标题旁边添加部分中的行数作为标签,例如 TODAY (6) , TOMORROW (17)..我可以在这个方法中实现这个吗?
    • 这在你的 iOS 9 中被贬值了。
    • @AnkishJain dequeueReusableHeaderFooterViewWithIdentifier: 在文档中似乎没有被弃用。你指的是什么?
    【解决方案3】:

    @codercat 答案的 Swift 实现:

    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:) 是一个更好的解决方案?
    【解决方案4】:

    通过以下方式制作自定义 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 方法来保持当前方法来创建节行和标题...
    • 为什么具体来说,它可以解决你的问题 viewForHeaderInSection 方法:id theSection = [[self.fetchedResultsController section]objectAtIndex:section];通过这个 Fetched results Array 获取任何值并设置标签文本。
    • 您的意思是我可以在 viewForHeaderInSection 方法中使用 titleForHeaderInSection 中的相同代码行,然后添加您的建议以格式化文本和背景颜色?
    • 仅用于从 NSFetchResultsSection 获取获取的结果...并从中设置标签标题。简单。
    【解决方案5】:

    我认为在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,它不可重用。
    • 在我看来,我的代码一切都很好,UIView 不应该是可重用的。
    • 什么样的错误?您不应该一对一地复制该代码,这是您自定义的提示,例如TABLE_HEADER_HEIGHT 和 TABLE_HEADER_TITLE_COLOR 由 me 值定义。
    猜你喜欢
    • 2011-06-19
    • 2015-03-25
    • 1970-01-01
    • 2020-01-15
    • 2021-06-11
    • 2012-08-20
    • 1970-01-01
    • 1970-01-01
    • 2013-10-02
    相关资源
    最近更新 更多