[SWIFT version] This problem had a UITableView UITableViewStylePlain problem, that is, setting the header font in
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) {...}
does not affect. Here is the code from my UITableViewController subclass that worked for me [verified with Xcode 6.4, iOS 8.4], see http://www.elicere.com/mobile/swift-blog-2-uitableview-section-header-color/
override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { let header = view as? UITableViewHeaderFooterView //recast your view as a UITableViewHeaderFooterView if (header == nil) { return; } if (myHeaderFont != nil) { header!.textLabel.font = myHeaderFont; } }
Heading height must be manually adjusted:
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { if (myHeaderFont == nil) { return 20; //DEFAULT_HEADER_HEIGHT_IN_POINTS; } return myHeaderFont.pointSize * 2; //HEIGHT_REL_TO_FONT;
}
The rest was standard, but the completeness is shown here:
override func viewDidLoad() { //... //https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewHeaderFooterView_class/index.html#//apple_ref/doc/uid/TP40012241 self.tableView.registerClass(UITableViewHeaderFooterView.self, forHeaderFooterViewReuseIdentifier: "HEADER_REUSE_ID") //... } override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { var header = tableView.dequeueReusableHeaderFooterViewWithIdentifier("HEADER_REUSE_ID") as? UITableViewHeaderFooterView; if (header == nil) { header = UITableViewHeaderFooterView(reuseIdentifier: "HEADER_REUSE_ID"); } header!.textLabel.text = myTitle; return header!; }
Dmitry Konovalov
source share