1

I'm basically trying to do what's in this link

How to Implement Custom Table View Section Headers and Footers with Storyboard

In my storyboard, I've embedded static cells in my TableViewController

I selected one of my table view cells and set the identifier as "CustomHeader" (at the storyboard)

and below is a snippet of my code

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        var headerView:UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("CustomHeader") as? UITableViewCell

        if (headerView == nil){
            println("errrrrrr")
        }
        return headerView
    }
    override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 30
    }

However, console keeps printing "errrrrrr" meaning that headerView is nil.

But I think whose identifier is "CustomHeader" cannot be nil because I manually selected a cell and set its identifier as CustomHeader in the storyboard!!

I'm new to iOS so I don't get what the problem is.

Any help is appreciated.

Thanks in advance!

Community
  • 1
  • 1
kwmaeng
  • 631
  • 2
  • 5
  • 20

1 Answers1

7

To dequeue a cell from a storyboard, you need to use the method which takes an index path. That is, don't use:

dequeueReusableCellWithIdentifier(_:)

Instead, use:

dequeueReusableCellWithIdentifier(_:forIndexPath:)

The hacky approach in the question you linked to no longer works. This method returns a header. Don't dequeue a cell.

You should use registerNib(_:forHeaderFooterViewReuseIdentifier:) or registerClass(_:forHeaderFooterViewReuseIdentifier:), and dequeueReusableHeaderFooterViewWithIdentifier(_:), as documented.

Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
  • Thanks!! Without you telling me the approach in my link is no longer valid, I would have been wasting a lot of time!! I implemented in other way! – kwmaeng Jan 09 '15 at 14:10