更改TableView标题文本颜色

时间:2012-07-02 17:36:49

标签: iphone ios cocoa-touch

我想在tableView中进行一个简单的更改。我有自定义表视图背景,我想更改页眉和页脚TEXT颜色,

仅限文字颜色。

我在互联网上看到,通常我们必须使用委托方法为表格视图提供一个全新的视图但我只能更改文本颜色(如果可能的话,还要改变阴影)......

有一种简单的方法吗避免创建新的整个视图

请帮帮我。

谢谢

4 个答案:

答案 0 :(得分:4)

如果您希望以除自定义字符串之外的任何方式自定义页眉/页脚,则需要创建UIView。

UITableViewDataSource文档中记录了这一点:

  

的tableView:titleForHeaderInSection:

     

<强>讨论   表视图使用固定的字体样式用于节标题标题。如果需要不同的字体样式,请在委托方法tableView:viewForHeaderInSection中返回自定义视图(例如,UILabel对象):

答案 1 :(得分:2)

很抱歉,自定义标题字体颜色的唯一方法是在委托方法中创建UIView

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

如果您查看here发布的答案,那么您可以看到一种非常简单的方法将其实现到您的应用程序中。您所要做的就是将该函数复制并粘贴到表视图的数据源中,然后将UILabel属性更改为您希望的任何属性。

以下是该帖子的代码,以便于参考:

Originally posted by Harsh:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *tempView=[[UIView alloc]initWithFrame:CGRectMake(0,200,300,244)];
    tempView.backgroundColor=[UIColor clearColor];

    UILabel *tempLabel=[[UILabel alloc]initWithFrame:CGRectMake(15,0,300,44)];
    tempLabel.backgroundColor=[UIColor clearColor]; 
    tempLabel.shadowColor = [UIColor blackColor];
    tempLabel.shadowOffset = CGSizeMake(0,2);
    tempLabel.textColor = [UIColor redColor]; //here u can change the text color of header
    tempLabel.font = [UIFont fontWithName:@"Helvetica" size:fontSizeForHeaders];
    tempLabel.font = [UIFont boldSystemFontOfSize:fontSizeForHeaders];
        tempLabel.text=@"Header Text";

    [tempView addSubview:tempLabel];

    [tempLabel release];
    return tempView;
}

答案 2 :(得分:0)

如果你设置没有UIView子类的页眉/页脚,你传递的是NSString,那么目前无法完成你想要的而不必创建UIView }。

答案 3 :(得分:0)

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    let headerView = view as! UITableViewHeaderFooterView
    headerView.textLabel?.textColor = ...
}

我什至可以为这个标签设置属性文本