之前就知道
可以通过直接实现:
viewForHeaderInSection
去设置对应的字体和颜色
然后去搜:
swift uitableview section font
发现还有其他办法:
swift ios 8 change font title of section in a tableview – Stack Overflow
swift – How do I set Section attributes in UITableView? – Stack Overflow
然后去用:
override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header = view as! UITableViewHeaderFooterView
header.textLabel?.font = UIFont(name: "Futura", size: 38)!
header.textLabel?.textColor = UIColor.lightGrayColor()
}
结果出错:
CustomerDetailViewController.swift:314:19: Method does not override any method from its superclass
如图:
-》猜测是:
是变成代理了?
所以去掉overvide试试:
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header = view as! UITableViewHeaderFooterView
header.textLabel?.font = UIFont(name: "Futura", size: 38)!
header.textLabel?.textColor = UIColor.lightGrayColor()
}
果然可以了。
再去看看定义,果然是有的:
public protocol UITableViewDelegate : NSObjectProtocol, UIScrollViewDelegate {
// Display customization
@available(iOS 2.0, *)
optional public func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath)
@available(iOS 6.0, *)
optional public func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int)
最后自己用:
let CustomerDetailSectionHeaderColor:UIColor = UIColor(hexString: "#999999")!
let CustomerDetailSectionHeaderFont:UIFont = UIFont.systemFontOfSize(12)
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
guard let sectionHeaderView = view as? UITableViewHeaderFooterView else {
return
}
sectionHeaderView.textLabel?.font = CustomerDetailSectionHeaderFont
sectionHeaderView.textLabel?.textColor = CustomerDetailSectionHeaderColor
}
实现了对应的效果: