22

Here is what I have at the moment.

enter image description here

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?

AMAN77
  • 6,218
  • 9
  • 45
  • 60
  • Yes you need to add the label in viewForHeaderInSection. – Janmenjaya Sep 21 '16 at 10:44
  • Hye man how did you add the alphabet on the right? –  Jan 07 '18 at 03:31
  • Its called an index list. Heres a vid https://www.youtube.com/watch?v=xYSKHna1KJk. Or another link with a great tutorial. http://www.edumobile.org/ios/indexed-table-views-in-swift/ – AMAN77 Jan 08 '18 at 07:36

6 Answers6

64

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

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
10

One liner solution (using optional chaining):

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    (view as? UITableViewHeaderFooterView)?.textLabel?.textColor = UIColor.red
}
Federico Zanetello
  • 3,321
  • 1
  • 25
  • 22
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()
}
Bista
  • 7,869
  • 3
  • 27
  • 55
3

Swift Solution

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    view.tintColor = UIColor.red
    let header = view as! UITableViewHeaderFooterView
    header.textLabel?.textColor = UIColor.white
}
Raj Joshi
  • 2,669
  • 2
  • 30
  • 37
1

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
}
Konrad77
  • 2,515
  • 1
  • 19
  • 36
0

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
    }
}
Jonny
  • 1,969
  • 18
  • 25