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

时间:2014-01-14 21:32:22

标签: ios uitableview

经过长时间的阅读和检查代码,我很自豪有一个自定义的表视图,其中包含部分和部分标题,所有部分都来自核心数据对象。现在我需要自定义部分标题和背景颜色。我已经看到它已经完成,但采用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 个答案:

答案 0 :(得分:23)

简单和优化

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

}

答案 1 :(得分:10)

这是一个使用现有代码设置标题文本的示例,但允许您使用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,以进一步自定义外观。

答案 2 :(得分:9)

迅速实施@ 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.lightGrayColor()

    var header : UITableViewHeaderFooterView = view as UITableViewHeaderFooterView
    header.textLabel.textColor = UIColor.redColor()

}

答案 3 :(得分:1)

通过以下方式制作自定义标题:

-(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。

答案 4 :(得分: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;
}

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