简体   繁体   中英

Swift: Changing TableView Header text color - Crashing

Using Swift 3, I'm trying to change the Section's Header color programmatically.

How do I change the backgroundColor to white and Text Color to black?

The sections header changes color but no text appears and then when I add code to change the header text color it crashes

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't add self as subview'

Swift Code

// Title for Header
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

    // Section Names
    let sectionNames = ["Followed Blogs", "All Blogs"]

    return sectionNames[section]
}

// View for Header
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let headerView = UIView()

    let headerLabel = UILabel()
    let sectionNames = ["Followed Blogs", "All Blogs"]
    headerLabel.text = sectionNames[section]
    headerLabel.frame = CGRect(x: 45, y: 5, width: 100, height: 35)
    headerLabel.addSubview(headerLabel)

    if (section == 0) {

        headerView.backgroundColor = UIColor.green
        headerLabel.textColor = UIColor.black
    } else {

        if darkMode {

            headerView.backgroundColor = UIColor.white
            headerLabel.textColor = UIColor.black

        } else {

            headerView.backgroundColor = UIColor.black
            headerLabel.textColor = UIColor.white
        }
    }

    return headerView
}

// Height for Section
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

    return 45
}

headerLabel.addSubview(headerLabel) is adding the label to self, which is the source of your error

Based on my understanding of your code, you should probably be using headerView.addSubview(headerLabel) instead

The text "Followed Blogs" doesn't fit it shows as "Followed B..."

This is (most likely) a layout issue, I'd personally investigate adding auto layout constraints to the label so that it binds to the top/bottom/left/right margins of the parent view

This is just to add on MadProgrammer's answer. I think instead of UIView you should use UITableViewHeaderFooterView

usage:

  tableViewInstance.register(UITableViewHeaderFooterView.self, forHeaderFooterViewResuseIdentifier: "headerIdentifier")

Then in viewForHeaderInSection :

let tableViewHeader = tableview.dequeueReusableHeaderFooterView(withIdentifier: "headerIdentifier")

btw, regarding the text "Followed Blogs" not fitting in its because of your label's width is too small for the current font. Why not add a constraints like this:

 headerView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-5-[label]-5-|",
                                                                   options: [],
                                                                   metrics: nil,
                                                                     views: ["tableView": headerLabel]))

 headerView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-5-[label]-5-|",
                                                                   options: [],
                                                                   metrics: nil,
                                                                     views: ["tableView": headerLabel]))

You make your tableView's headerHeight be dynamic

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM