[英]swift change section array element text or font color in tableview header
My tableview code is -我的tableview代码是-
import UIKit
class ViewController: UIViewController {
var categories = ["Action", "Drama", "Science Fiction", "Kids", "Horror"]
}
extension ViewController : UITableViewDelegate { }
extension ViewController : UITableViewDataSource {
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return categories[section]
}
func numberOfSections(in tableView: UITableView) -> Int {
return categories.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CategoryRow
return cell
}
}
The result on the simulator is -模拟器上的结果是——
Now, I want the to customize the font/text of the various sections ie Action, Drama, Science Fiction, Kids, Horror other than black, may be make it bigger etc. How is it possible ?现在,我想要自定义各个部分的字体/文本,即动作、戏剧、科幻、儿童、恐怖以外的黑色,可能会变大等。这怎么可能?
With tableView(_:titleForHeaderInSection:)
method, you can't modify the UI properties of the header text.使用tableView(_:titleForHeaderInSection:)
方法,您无法修改标题文本的 UI 属性。
You need to implement tableView(_:viewForHeaderInSection:)
and tableView(_:heightForHeaderInSection:)
methods to get a custom UI for the header, ie您需要实现tableView(_:viewForHeaderInSection:)
和tableView(_:heightForHeaderInSection:)
方法来获取标题的自定义 UI,即
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let label = UILabel()
label.text = categories[section]
label.font = UIFont.systemFont(ofSize: 20.0, weight: .bold)
label.textColor = .red
label.sizeToFit()
return label
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50.0
}
Simply return a UILabel
instance with required text attributes in tableView(_:viewForHeaderInSection:)
只需在tableView(_:viewForHeaderInSection:)
返回一个具有所需文本属性的UILabel
实例
There is below function available in UITableViewDelegate
UITableViewDelegate
有以下功能可用
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
<#code#>
}
You would have the full control over the header of each section and customisation can be done accordingly.您可以完全控制每个部分的标题,并可以相应地进行自定义。
In the method在方法中
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return categories[section]
the table view uses a fixed font style for section header titles.表格视图为节标题标题使用固定字体样式。 If you want a different font style, return a custom view (for example, a UILabel object) in the delegate method tableView(_:viewForHeaderInSection:) instead.如果您想要不同的字体样式,请在委托方法 tableView(_:viewForHeaderInSection:) 中返回自定义视图(例如,UILabel 对象)。
You can use delegate method of UITableView
tableView(_:viewForHeaderInSection:) to get custom UI for the header您可以使用UITableView
tableView(_:viewForHeaderInSection:) 的委托方法来获取标题的自定义 UI
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView(frame: CGRect(x: 0, y: 0, width: yourTable.bounds.width, height: 40))
let headerLabel = UILabel(frame: CGRect(x: 15, y: 0, width: yourTable.bounds.width, height: 40))
headerLabel.font = UIFont.boldSystemFont(ofSize: 20)
headerLabel.textColor = .blue
headerLabel.text = self.tableView(self.yourtableView, titleForHeaderInSection: section)
headerLabel.sizeToFit()
headerView.addSubview(headerLabel)
return headerView
}
And height for section和截面高度
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 30 //whatever you want
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.