Home > Net >  Why section header always showing in grey colour and in one line? in Swift
Why section header always showing in grey colour and in one line? in Swift

Time:08-25

I am using two tableviews side by side, so need section header text in two lines and in black colour. but its not coming like that.. why?

code: I have used these methods but still not showing header text in black colour and in two lines... please let me know, how to show header text in black and in two lines

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    35
 }

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    (view as! UITableViewHeaderFooterView).textLabel?.textColor = UIColor.black
    (view as! UITableViewHeaderFooterView).textLabel?.numberOfLines = 2

}

func numberOfSections(in tableView: UITableView) -> Int {
    if tableView == tableView1 {
        return nonProSubategory.count
    } else if tableView == tableView2 {
        return proSubategory.count
    }
    return 0
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    if tableView == tableView1 {
        return nonProSubategory[section].title
    } else if tableView == tableView2 {
        return proSubategory[section].title
    }
    return ""
}

o/p: with this code text always showing in grey colour and in one line.. here "beauty,health.." and "home services" are header text

Screen of header colour in grey and one line

EDIT:

if i use only these two methods:

 func numberOfSections(in tableView: UITableView) -> Int {
    if tableView == tableView1 {
        return nonProSubategory.count
    } else if tableView == tableView2 {
        return proSubategory.count
    }
    return 0
}
private func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let v = UILabel()
    v.textColor = .black
    v.numberOfLines = 2

    // you can set a font here, if desired
    //  for example:
    //v.font = .italicSystemFont(ofSize: 15)

    if tableView == tableView1 {
        v.text = nonProSubategory[section].title
    } else if tableView == tableView2 {
        v.text = proSubategory[section].title
    }
    return v
}

o/p: here header title("beauty,health.." and "home services") is missing..

edit code o/p

CodePudding user response:

You could take your approach... however...

First, you should always properly cast classes and unwrap optionals:

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    if let v = view as? UITableViewHeaderFooterView,
       let label = v.textLabel
    {
        label.textColor = .black
        label.numberOfLines = 2
    }
}

The default .textLabel has been deprecated though, plus I don't think .numberOfLines is going to work.

Much better approach is to return your own view for the section header.

This example uses a UILabel as the view to return, but you can return a more complex view with various subviews, if you wanted to:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    
    let v = UILabel()
    v.textColor = .black
    v.numberOfLines = 2
    
    // you can set a font here, if desired
    //  for example:
    //v.font = .italicSystemFont(ofSize: 15)
    
    if tableView == tableView1 {
        v.text = nonProSubategory[section].title
    } else if tableView == tableView2 {
        v.text = proSubategory[section].title
    }
    return v
}

Then get rid of titleForHeaderInSection, willDisplayHeaderView and heightForHeaderInSection (height will be handle be auto-layout).

CodePudding user response:

There are multiple ways to add section headers to a table view like having it inside UITableView if its inside UIViewController in a Storyboards or in an external custom view like nibs. Include how did you add your section header in the code. Try to use story boards and automatic dimensions for this. Yes, you can restrict no of lines if needed.

Some samples

https://santoshkumarjm.medium.com/how-to-create-custom-header-and-footer-view-for-tableview-using-xib-in-swift-23959d81d5c4

https://developer.apple.com/documentation/uikit/views_and_controls/table_views/adding_headers_and_footers_to_table_sections

  • Related