如何更改表格视图中节标题的颜色? [英] How do you change the colour of a section title in a tableview?

查看:202
本文介绍了如何更改表格视图中节标题的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我现在的位置.

我该如何引用它,以便可以更改文本颜色以匹配我的索引列表? sectionForSectionIndexTitle 可以很好地添加正确的节标题,但是如何准确地访问title元素呢?

How do I refer to this so that I can change the text colour to match my index list? The sectionForSectionIndexTitle worked well for adding in the correct section title but how exactly does one access the title element?

还是不可能,我需要重新绘制视图并使用 viewForHeaderInSection 添加它?

Or is it impossible and I need to redraw the view and add it with viewForHeaderInSection?

推荐答案

您可以使用UITableViewDelegate的方法之一

swift3及更高版本

  func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    if let headerView = view as? UITableViewHeaderFooterView {
        headerView.contentView.backgroundColor = .white
        headerView.backgroundView?.backgroundColor = .black
        headerView.textLabel?.textColor = .red
    }
}

目标C

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    if([view isKindOfClass:[UITableViewHeaderFooterView class]]){
        UITableViewHeaderFooterView * headerView = (UITableViewHeaderFooterView *) view;
        headerView.textLabel.textColor  = [UIColor RedColor];  
    }
}

作为参考,我从此处获得了模型答案

for Reference I taken the model answer from here

这篇关于如何更改表格视图中节标题的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆