title: Reusing header sections in tableview
body: <p>I've been searching the whole WWW for a decent solution, but most seem outdated or insufficient.</p>
  
  <p>I'm struggling in simply reusing a custom UITableViewHeaderFooterView in my UITableViewController.</p>
  
  <p>This is my approach:</p>
  
  <ol>
  <li>Create a nib.</li>
  <li>Create a custom <code>UITableViewHeaderFooterView</code>, named <code>HeaderView</code></li>
  <li>Add a label and connect it to <code>HeaderView</code></li>
  <li>Register the nib in <code>UITableViewController</code>'s <code>viewDidLoad()</code></li>
  <li>Dequeue and return the nib in <code>tableView(_ tableView: UITableView, viewForHeaderInSection section: Int)</code></li>
  </ol>
  
  <p>Custom <code>HeaderView</code> class:</p>
  
  <pre><code>class HeaderView: UITableViewHeaderFooterView {
      @IBOutlet weak var customLabel: UILabel!
  }
  </code></pre>
  
  <p>In <code>UITableViewController</code> class:</p>
  
  <pre><code>override func viewDidLoad() {
      super.viewDidLoad()
  
      tableView.register(UINib(nibName: "HeaderView", bundle: nil), forHeaderFooterViewReuseIdentifier: "Header")
  
  }
  
  override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -&gt; UIView? {
      let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "Header") as! HeaderView
      headerView.customLabel.text = "This is a header section"
      return headerView
  }
  </code></pre>
  
  <p>This seems to be the approach advised by many, but I believe I'm unable to properly set the nib's owner custom class to <code>HeaderView</code> (<code>UITableViewHeaderFooterView</code>). When the cell is dequeued I get a fatal crash:</p>
  
  <p><code>*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[&lt;NSObject 0x60000020ebe0&gt; setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key customLabel.'</code></p>
  
  <p>I'm definitely sure I've connected the UILabel correctly to File's Owner in nib IB.</p>
  
  <p><a href="https://i.stack.imgur.com/kq91h.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kq91h.png" alt="Assigning class"></a></p>
  
  <p>I tried changing the <code>HeaderView</code> class to <code>UIView</code>, but then <code>dequeueReusableHeaderFooterView(withIdentifier: "Header") as! HeaderView</code> would result in a crash when forcing UITableViewHeaderFooterView to UIView.</p>
  
  <p>Why is this, and what is the current way in solving this?</p>