How do you change the colour of a section title in a tableview?

こ雲淡風輕ζ 提交于 2019-12-05 10:48:59

问题


Here is what I have at the moment.

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?

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


回答1:


you can use the one of UITableViewDelegate's method

swift3 and above

  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
    }
}

objective 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




回答2:


One liner solution (using optional chaining):

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    (view as? UITableViewHeaderFooterView)?.textLabel?.textColor = UIColor.red
}



回答3:


Custom Title:

override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    let title = UILabel()
    title.font = UIFont(name: "SFUIDisplay-Light", size: 13)!
    title.textColor = UIColor.redColor()

    let header = view as! UITableViewHeaderFooterView
    header.textLabel!.font=title.font
    header.textLabel!.textColor=title.textColor
    header.contentView.backgroundColor = UIColor.whiteColor()
}



回答4:


I would use the Appearance() proxy class. I usually add them in a function in AppDelegate and call them didFinishLaunching.

private func setupApperances() {
    UILabel.appearance(whenContainedInInstancesOf: [UITableViewHeaderFooterView.self]).textColor = .red
}



回答5:


You can make your own section title (header/footer) view, and it is easy.

class BlackTableViewHeaderFooterView : UITableViewHeaderFooterView {

    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)

        contentView.backgroundColor = .black

        textLabel?.font = UIFont.preferredFont(forTextStyle: .body)
        textLabel?.numberOfLines = 0
        textLabel?.textColor = .white
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class TableViewController : UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(BlackTableViewHeaderFooterView.self, forHeaderFooterViewReuseIdentifier: "\(BlackTableViewHeaderFooterView.self)")
        // do other setup
    }

    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "\(BlackTableViewHeaderFooterView.self)")
        header.textLabel?.text = "" // set your header title
        return header
    }
}


来源:https://stackoverflow.com/questions/39614268/how-do-you-change-the-colour-of-a-section-title-in-a-tableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!