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

Ios Swift:更改TableView标题文本颜色-崩溃,ios,swift,uitableview,Ios,Swift,Uitableview,使用Swift 3,我试图以编程方式更改节的标题颜色 如何将背景颜色更改为白色,将文本颜色更改为黑色 节标题会更改颜色,但不会显示任何文本,当我添加代码以更改标题文本颜色时,它会崩溃 由于未捕获异常而终止应用程序 “NSInvalidArgumentException”,原因:“无法将自身添加为子视图” Swift代码 // Title for Header func tableView(_ tableView: UITableView, titleForHeaderInSection sect

使用Swift 3,我试图以编程方式更改节的标题颜色

如何将背景颜色更改为白色,将文本颜色更改为黑色

节标题会更改颜色,但不会显示任何文本,当我添加代码以更改标题文本颜色时,它会崩溃

由于未捕获异常而终止应用程序 “NSInvalidArgumentException”,原因:“无法将自身添加为子视图”

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
}

headerLabel.addSubview(headerLabel)
正在将标签添加到self,这是错误的根源

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

文本“Followed Blogs”不适合显示为“Followed B…”


这(很可能)是一个布局问题,我个人会研究向标签添加自动布局约束,使其绑定到父视图的上/下/左/右页边距

这只是为了补充程序员的答案。我认为应该使用
UITableViewHeaderFooterView

用法:

  tableViewInstance.register(UITableViewHeaderFooterView.self, forHeaderFooterViewResuseIdentifier: "headerIdentifier")
然后在标题部分的视图中:

let tableViewHeader = tableview.dequeueReusableHeaderFooterView(withIdentifier: "headerIdentifier")
顺便说一句,关于文本
“Followed Blogs”
不适合它,因为标签的宽度对于当前字体来说太小。为什么不添加这样的约束:

 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]))

您将tableView的headerHeight设置为动态的

headerLabel.addSubview(headerLabel)
将导致您的问题,您的意思是
headerView.addSubview(headerLabel)
?是的,修复了它,谢谢!介意帮我解决这个问题吗?文本“Followed Blogs”不适合显示为“Followed B…”。我的第一个想法是对标签应用一些自动布局约束,以确保它占据了所需的视图空间,我在这方面的经验非常有限,尽管您可以添加答案,所以我给您评分