Title: TableView FooterView not finding registered nib
Tags: ios;swift;uitableview;xib
Question: I'm using a custom xib that contains a few buttons and labels that I want to load into the footer of a UITableView.
However, I get an error when it is trying to go to the table in the app:
fatal error: unexpectedly found nil while unwrapping an Optional value
The tableview has 5 other dynamic cells which are created in:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell
I can't figure out what is happening.
I've registered a footer nib as such in the viewDidLoad.
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "Proxima Nova", size: 17)!]
self.navigationController?.navigationBar.barTintColor = UIColor(red: 79/255, green: 116/255, blue: 136/255, alpha: 1.0)
// create a longPressRecognizer that is used for bringing up the modal to select recipes and marking the meal as eaten
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(MealMapTableViewController.longPress(_:)))
self.view.addGestureRecognizer(longPressRecognizer)
navigationBar.title! = "Meal Map"
// This allows for the side menu to appear from within the app
if self.revealViewController() != nil {
menuButton.target = self.revealViewController()
menuButton.action = #selector(SWRevealViewController.revealToggle(_:))
let footerNib = UINib(nibName: "WaterTrackerTableViewCell", bundle: nil)
tableView.registerNib(footerNib, forCellReuseIdentifier: "WaterTrackerTableViewCell")
Then I have overriden the methods for creating the footers.
// MARK: - Section Footer Methods
override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -&gt; UIView? {
let footer = self.tableView.dequeueReusableHeaderFooterViewWithIdentifier("WaterTrackerTableViewCell") as! WaterTrackerTableViewCell
footer.amountLeftLabel.text = "testing"
return footer
The WaterTrackerTableViewCell is defined as such:
class WaterTrackerTableViewCell: UITableViewHeaderFooterView {
@IBOutlet var bottleButton8Oz: UIButton!
@IBOutlet var bottleButton16Oz: UIButton!
@IBOutlet var bottleButton24Oz: UIButton!
@IBOutlet var bottleButton32Oz: UIButton!
@IBOutlet var amountLeftLabel: UILabel!
@IBOutlet var goalLabel: UILabel!
@IBOutlet var waterProgressBar: UIProgressView!
If I try to include this method:
override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -&gt; CGFloat {
return 90.0
It crashes the app but if I exclude that method the footer does not appear. I tried it without my xib file
override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -&gt; UIView? {
let footerView = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 40))
footerView.backgroundColor = UIColor.blackColor()
return footerView
override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -&gt; CGFloat {
return 40.0
and that works.
update of error:
Image of the Error
Update 2:
I fixed what I was doing in the viewDidLoad
tableView.registerNib(footerNib, forCellReuseIdentifier: "WaterTrackerTableViewCell")
tableView.registerNib(footerNib, forHeaderFooterViewReuseIdentifier: "WaterTrackerTableViewCell")
but it is still crashing now I am getting another error:
2016-10-08 11:08:07.321 Cleanse App[3977:2893987] *** Terminating
app due to uncaught exception 'NSInternalInconsistencyException',
reason:
'Could not load NIB in bundle: 'NSBundle  (loaded)' with name 'WaterTrackerTableViewCell''
Update 3:
Xib WaterTrackerTableViewCell and file name
Comment: amountLeftLabel outlet is connected?

Comment: Have you set the class for the custom view?

Comment: yes, in the storyboard. like this: http://imgur.com/a/8jURr ?

Comment: Please show similar image in your case.  ..and what is the name of your xib file?

Comment: @ErikKOrbotron Although you managed to fix the issue by creating new files. If you could share the name of your xib file and complete crash log then we may find the reason behind this crash and it may help others too.

Comment: Yes it's connected. I added an image of where it is crashing.

Comment: Inside of the storyboard?

Comment: yea I have set it in there

Comment: Added update to post.

Comment: Update 3 has a screen shot of the file name(in the tab name) which is WaterTrackerTableViewCell.xib. I don't have the crash log any more unfortunately.


Here is another answer: I ended up deleting the Xib file and the swift file, recreating it and inserting it as a cell rather than a footer which seemed to work. I had made the Xib file before making the swift class file for it and it's possible they were not configured properly.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 10, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:10/08/16', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<ios><swift><uitableview><xib>', 'Title': 'TableView FooterView not finding registered nib', 'ViewCount': 432, 'id': 39935473, 'user_AboutMe': '', 'user_AccountId': 7701438.0, 'user_DisplayName': 'Erik Karlsson', 'user_Reputation': 1.0, 'user_Views': 11.0, 'user_id': 5833851.0}}