dequeueReusableCellWithIdentifier 一直返回 nil

dequeueReusableCellWithIdentifier keeps returning nil

我基本上是在尝试执行此链接中的内容

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

在我的故事板中,我在 TableViewController 中嵌入了静态单元格

我选择了一个 table 视图单元格并将标识符设置为 "CustomHeader"(在故事板中)

以下是我的代码片段

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
    }

然而,控制台继续打印 "errrrrrr" 意味着 headerView 为 nil。

但我认为其标识符"CustomHeader"不能为nil,因为我手动选择了一个单元格并在故事板中将其标识符设置为CustomHeader!!

我是 iOS 的新手,所以我不明白问题是什么。

感谢任何帮助。

提前致谢!

要从故事板中取出单元格,您需要使用采用索引路径的方法。也就是说,不要使用:

dequeueReusableCellWithIdentifier(_:)

改为使用:

dequeueReusableCellWithIdentifier(_:forIndexPath:)

您链接到的问题中的 hacky 方法不再有效。这个方法returns一个header。不要使单元格出队。

您应该按照记录使用 registerNib(_:forHeaderFooterViewReuseIdentifier:)registerClass(_:forHeaderFooterViewReuseIdentifier:)dequeueReusableHeaderFooterViewWithIdentifier(_:)