[英]How to change the section header text color in UITableView
I've been actively looking for a solution to do this, and the closest I got was this:我一直在积极寻找解决方案来做到这一点,我得到的最接近的是:
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header = view as! UITableViewHeaderFooterView
header.textLabel?.textColor = UIColor.white
}
I put this function in my TableViewController, as I understand it's a UITableViewDelegate method.我把这个 function 放在我的 TableViewController 中,据我所知这是一个 UITableViewDelegate 方法。 For context, the UITableViewController I'm using is embedded into a Container View within a UIView.对于上下文,我使用的 UITableViewController 嵌入到 UIView 内的容器视图中。 Nothing happens.什么都没发生。 Why is this?为什么是这样?
How did you register your header view cell in Section of TableView??您是如何在 TableView 部分中注册 header 视图单元格的? Please show more code请显示更多代码
I recommend that you should implement your table header view cell in Section like the following steps:我建议您应该按照以下步骤在 Section 中实现表 header 视图单元格:
Step 1 : Create YourTableHeaderViewCell第 1 步:创建YourTableHeaderViewCell
Step 2 : Add text label in YourTableHeaderViewCell.xib第 2 步:在 YourTableHeaderViewCell.xib 中添加文本label
Step 3 : Create the outlet text label in YourTableHeaderViewCell.swift第 3 步:在 YourTableHeaderViewCell.swift 中创建出口文本label
It became like:它变成了:
class YourTableHeaderViewCell: UITableViewCell {
// MARK: - Outlets
@IBOutle weak var textLabel: UILabel!
// MARK: - Variables
static var identifier = "YourTableHeaderViewCell"
}
Step 4 : Setup YourViewController and Register your table header view cell:第 4 步:设置YourViewController并注册您的表 header 视图单元:
class YourViewController: UIViewController {
// MARK: - View Cycle
override func viewDidLoad() {
super.viewDidLoad()
setupTableView()
}
private func setupTableView() {
tableView.delegate = self
tableView.dataSource = self
tableView.separatorStyle = .none
tableView.backgroundColor = UIColor.clear
tableView.register(UINib.init(nibName: YourTableViewHeaderView.identifier, bundle: nil), forCellReuseIdentifier: YourTableViewHeaderView.identifier) // register your table header view cell
tableView.register(UINib.init(nibName: YourTableViewCell.identifier, bundle: nil), forCellReuseIdentifier: YourTableViewCel.identifier) // register your table view cell
}
}
// MARK: - UITableViewDataSource
extension YourViewController: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return // number of your sections
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return // number of rows in sections
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: YourTableViewCell.identifier, for: indexPath) as? YourTableViewCell {
return cell
}
return UITableViewCell()
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if let cell = tableView.dequeueReusableCell(withIdentifier: YourTableViewHeaderView.identifier) as? YourTableViewHeaderView {
cell?.textLabel?.textColor = UIColor.white // Change the text color here
return cell.contentView
}
return UIView()
}
}
// MARK: - UITableViewDelegate
extension YourViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
}
Make sure you implement:确保您实施:
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "title"
}
And make you sure you are not implementing this at the same time:并确保您没有同时实现此功能:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
<#code#>
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.