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子类化以进一步自定义外观.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...