Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dequeueReusableHeaderFooterViewWithIdentifier is returning nil for cell, but dequeueReusableCellWithIdentifier isn't

I'm adding a footer to a table view using a prototype cell in Interface Builder:

enter image description here

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→

Update:

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
like image 977
brandonscript Avatar asked Dec 14 '22 08:12

brandonscript


2 Answers

Have you tried registering the NIB in code using:

tableView.registerNib(UINib(nibName: "MyFooter", bundle: nil), forHeaderFooterViewReuseIdentifier: "MyFooterID")
like image 177
paulvs Avatar answered May 06 '23 10:05

paulvs


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.

like image 38
samwize Avatar answered May 06 '23 09:05

samwize