I am running into a weird issue when creating a customHeader for my tableView. the error that I am receiving is the following:
nib must contain exactly one top level object which must be a UITableViewHeaderFooterView instance
I ran through my code and xib file as well as examples but I couldnt find anything wrong any ideas I am missing?
The header Xib was created with a regular view and then a sub view with a label, the class was created with the following:
CustomTableHeader Class:
class CustomTableHeader: UITableViewHeaderFooterView {
static var CustomTableHeaderIdentifier = "CustomTableHeader"
@IBOutlet weak var titleLabel: UILabel!
override awakeFromNib(){
super.awakeFromNib()
self.titleLabel.text = "Header"
}
override func prepareForReuse() {
super.prepareForReuse()
}
}
ViewController Class:
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(UINib.init(nibName: CustomTableHeader.CustomTableHeaderIdentifier, bundle: nil), forHeaderFooterViewReuseIdentifier: CustomTableHeader.CustomTableHeaderIdentifier)
}
//Other Methods
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: CustomTableHeader.CustomTableHeaderIdentifier) as? CustomTableHeader {
header.titleLabel.text = "Section 1"
return header
}
}
I figured out my issue! Just in case anybody ever runs into this, make sure that your first view is truly the only object in the XIB file. For example on mine I was trying to add a gesture to the header view and this is considered an object as well. If you want to add a gesture you can do so programmatically:
addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(/* FUNCTION */)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With