Swift:更改TableView标题文本颜色 - 崩溃

时间:2022-11-04 21:07:50

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

使用Swift 3,我试图以编程方式更改Section的Header颜色。

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

如何将backgroundColor更改为白色,将Text Color更改为黑色?

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'

因未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'无法将self添加为子视图'

Swift Code

SWIFT代码

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

2 个解决方案

#1


1  

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

headerLabel.addSubview(headerLabel)将标签添加到self,这是您的错误的来源

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

根据我对您的代码的理解,您可能应该使用headerView.addSubview(headerLabel)

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

“Followed Blogs”文本不适合显示为“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

这(很可能)是布局问题,我个人会调查将自动布局约束添加到标签,以便它绑定到父视图的顶部/底部/左/右边距

#2


0  

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

这只是为了补充MadProgrammer的答案。我认为你应该使用UITableViewHeaderFooterView代替UIView

usage:

用法:

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

Then in viewForHeaderInSection:

然后在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

你使tableView的headerHeight变得动态

#1


1  

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

headerLabel.addSubview(headerLabel)将标签添加到self,这是您的错误的来源

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

根据我对您的代码的理解,您可能应该使用headerView.addSubview(headerLabel)

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

“Followed Blogs”文本不适合显示为“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

这(很可能)是布局问题,我个人会调查将自动布局约束添加到标签,以便它绑定到父视图的顶部/底部/左/右边距

#2


0  

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

这只是为了补充MadProgrammer的答案。我认为你应该使用UITableViewHeaderFooterView代替UIView

usage:

用法:

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

Then in viewForHeaderInSection:

然后在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

你使tableView的headerHeight变得动态