hi Luca_65,
so, full disclosure: i am using some custom section headers, but simply overriding tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) to create the view in code. it's a simple enough header - i really just want a background color, a centered label, and not much more.
i have usually stayed away from nib files for custom views and things.
but, after i mentioned the NSHipster article, i tried to follow its blueprint and ran into some frustration, which sounds about the same as what you are experiencing. and, much as I love the Moeykens videos, i use the same style heared shared across multiple tables, and i don't want to reinvent the header using a UITableViewCell in each table.
after seeing your recent post, i felt is should go back and try again, and after a little extra frustration, finally got something that works. (it's a hybrid of some of what you've seen in this discussion):
(1) create a UITableViewCell class to define your header, e.g., TableViewSectionHeaderCell.swift (yes, create a subclass of UITableViewCell, not a subclass of the TableViewHeaderFooterView class or the UIView class)
(2) also add it in with its own nib, e.g., TableViewSectionHeaderCell.xib. (your .xib should have just a custom UITableViewCell dragged out into it, which is what you see in the NSHipster picture.) be sure that you've got the class of the TableViewCell and the File's Owner set correctly on this. design away, create outlets as usual for your class.
ADDED IN LATER EDIT: also be sure you set the cells ReuseIdentifier to your class name (in my case, TableViewSectionHeaderCell).
(3) when you need a header in a UITableView from this nib, be sure to register it for whichever UITableView wants to use it in tableView.viewDidLoad, as in
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(TableViewSectionHeaderCell.nib, forCellReuseIdentifier: TableViewSectionHeaderCell.reuseIdentifier)
be sure to define the nib and reuse identifier in the class as in NSHipster, although i found that making the reuseIdentifier a class var rather than a static let (in the NSHipster articale) seemed to work better:
class TableViewSectionHeaderCell: UITableViewCell {
class var reuseIdentifier: String {
return String(describing: self)
}
class var nib: UINib {
return UINib(nibName: String(describing: self), bundle: nil)
}
// plus whatever else you want
(4) override tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) to load your new UITableViewCell class, set it up, and then return the contentView of the cell (my section header just sets the title by filling in a label i have defined in the cell's nib)
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
guard let cell = tableView.dequeueReusableCell(withIdentifier: TableViewSectionHeaderCell.identifier) as? TableViewSectionHeaderCell else {
return nil
}
cell.sectionTitle!.text = sectionTitleDictionary[sectionList[section]]!
return cell.contentView
}
ADDED IN LATER EDIT: TableViewSectionHeaderCell.identifier should be TableViewSectionHeaderCell.reuseIdentifier above.
(cell.sectionTitle is a label and the expression sectionTitleDictionary[sectionList[section]]! is just how i find the title of a section.)
you'll want to set the header's height with tableView(_ tableView: UITableView, heightForHeaderInSection section: Int).
i think i got all the important code aspects covered above (despite some struggles getting the code editor to really work right) -- questions appreciated going forward!
hope that helps,
DMG