[英]Swift: Changing TableView Header text color - Crashing
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为白色,文本颜色为黑色?
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”而终止应用程序,原因:“无法将自身添加为子视图”
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
}
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 (最可能是)布局问题,我将亲自调查向标签添加自动布局约束,以便将其绑定到父视图的顶部/底部/左侧/右侧边距
This is just to add on MadProgrammer's answer. 这只是补充MadProgrammer的答案。 I think instead of
UIView
you should use UITableViewHeaderFooterView
我想不是
UIView
你应该使用UITableViewHeaderFooterView
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. 顺便说一句,因为您的标签宽度对于当前字体而言太小,导致文本
"Followed Blogs"
不适合其内容。 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是动态的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.