答案 0 :(得分:17)
Swift 4
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Your Header Name"
}
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
header.textLabel?.font = UIFont(name: "YourFontname", size: 14.0)
header.textLabel?.textAlignment = NSTextAlignment.right
}
答案 1 :(得分:5)
您必须在UITableView数据源方法viewForHeaderInSection
中执行此操作:
Swift 2.3:
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerText = UILabel()
headerText.textColor = UIColor.lightGrayColor()
headerText.adjustsFontSizeToFitWidth = true
switch section{
case 0:
headerText.textAlignment = .Center
headerText.text = "This Header Will Be Centered"
case 1:
headerText.textAlignment = .Right
headerText.text = "This Header Will Be Aligned Right"
default:
headerText.textAlignment = .Left
headerText.text = "Default Will Be Left"
}
return headerText
}
编辑:修改了上面的代码。您可以使用section
参数来标识要修改的部分。