I'm adding a footer to a table view using a prototype cell in Interface Builder:
When instantiating the footer, dequeueReusableHeaderFooterViewWithIdentifier()
is returning nil for cell, but dequeueReusableCellWithIdentifier()
isn't. I've created a footer view with the correct identifier in Interface Builder:
And then I instantiate the footer:
func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let id = "MyFooter"
// this works:
// guard let footerCell = self.todaysChoresTableView.dequeueReusableCellWithIdentifier(id) else {
// return nil
// }
guard let footerCell = self.todaysChoresTableView.dequeueReusableHeaderFooterViewWithIdentifier(id) else {
// this doesn't, always returning nil
return nil
}
}
Why is dequeueReusableHeaderFooterViewWithIdentifier()
always returning nil?
(lldb) po self.tableView.dequeueReusableHeaderFooterViewWithIdentifier(id)
nil
(lldb) po self.tableView.dequeueReusableCellWithIdentifier(id)
▿ Optional(<UITableViewCell: 0x7fa313c835f0; frame = (0 0; 600 44); clipsToBounds = YES; layer = <CALayer: 0x7fa313c73150>>)
- Some : <UITableViewCell: 0x7fa313c835f0; frame = (0 0; 600 44); clipsToBounds = YES; layer = <CALayer: 0x7fa313c73150→
I've tried doing this too, and while now it isn't nil
anymore, it doesn't seem to actually build a view based on my IB design:
tableView.registerClass(UITableViewHeaderFooterView.self, forHeaderFooterViewReuseIdentifier: id)
let footerView: UITableViewHeaderFooterView? = self.todaysChoresTableView.dequeueReusableHeaderFooterViewWithIdentifier(id)
footerView!.backgroundColor = UIColor.redColor()
return footerView
Have you tried registering the NIB in code using:
tableView.registerNib(UINib(nibName: "MyFooter", bundle: nil), forHeaderFooterViewReuseIdentifier: "MyFooterID")
iOS SDK changed behaviour at some point.
As of iOS 10, my app dequeue which uses dequeueReusableHeaderFooterViewWithIdentifier
but it returns nil. Previously, in iOS 8 and 9, it dequeues properly.
My fix is to use dequeueReusableCellWithIdentifier
instead, and return the view with cell.contentView
.
Alternatively, use a nib and register it.
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