Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nib must contain exactly one top level object which must be a UITableViewHeaderFooterView instance

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
     }
}
like image 932
paul590 Avatar asked Jul 25 '17 21:07

paul590


1 Answers

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 */)))
like image 131
paul590 Avatar answered Sep 28 '22 12:09

paul590