-1

I'm new to Swift, and am trying to connect a custom UITableViewHeaderFooterView with a nib to my viewcontroller. When I do, I get this common error, which I've dealt with before and which is summarized in this post.

However this time, I'm a bit confused as to what optional is being unwrapped and hence how to fix the problem.

As far as I can tell everything is hooked up correctly-- the UITableView I'm using (menuTableView) is connect to the IBOutlet, as are all the parts of the custom header in the nib. The identifier and nib name are correct as well. Any idea what's going on? Here's the code where the error is originating:

menuTableView.register(UINib(nibName: "MenuTableHeader", bundle: nil), forHeaderFooterViewReuseIdentifier: "MenuTableHeader")

And here's my MenuTableHeader.swift file, minus a function or two.

class MenuTableHeader: UITableViewHeaderFooterView {


@IBOutlet var menuTableHeader: UIView!
@IBOutlet var menuHeaderLabel: UILabel?
@IBOutlet var menuHeaderImage: UIImageView?

var delegate: MenuTableHeaderDelegate?
var section: Int = 0


override init(reuseIdentifier: String?) {
    super.init(reuseIdentifier: reuseIdentifier)

    // Call tapHeader when tapping on this header

    addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(MenuTableHeader.tapHeader(_:))))
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

Thanks!

Community
  • 1
  • 1
Nate
  • 165
  • 8
  • Possible duplicate of [What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?](http://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) – rmaddy Apr 05 '17 at 01:39
  • I've read through that post, but I'm still confused as to how it applies to declaring a nib, since I'm not sure what the optional is in this case. – Nate Apr 05 '17 at 01:45
  • Can you show the Console window with the error? – Danh Huynh Apr 05 '17 at 01:55

1 Answers1

0

It seems to me that the nib name and the identifier should not be the same, even if there is only one element in the nib. For example, in my project, I used the following line:

tableView.register(UINib(nibName: "PostCellTextAndPic", bundle: nil), forCellReuseIdentifier: "PostCellTextAndPicId")

Rename the file and the identifier accordingly.

Acoop
  • 2,586
  • 2
  • 23
  • 39