微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

经过漫长的阅读和检查代码,我很自豪拥有一个自定义表视图,其中包含部分和部分标题,全部由核心数据对象填充.现在我需要自定义部分标题和背景颜色.我已经看到它完成,但在一个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;
    }

}

解决方法

以下是使用现有代码设置标题文本的示例,但可以使用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子类化以进一步自定义外观.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐