【问题标题】:dequeueReusableCellWithIdentifier keeps returning nildequeueReusableCellWithIdentifier 不断返回 nil
【发布时间】:2015-03-06 18:23:57
【问题描述】:

我基本上是在尝试做这个链接中的内容

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

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

我选择了一个表格视图单元格并将标识符设置为“CustomHeader”(在情节提要中)

下面是我的代码的sn-p

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”不能为零,因为我手动选择了一个单元格并将其标识符设置为情节提要中的 CustomHeader!!

我是 iOS 新手,所以我不明白问题出在哪里。

感谢任何帮助。

提前致谢!

【问题讨论】:

    标签: ios uitableview swift


    【解决方案1】:

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

    dequeueReusableCellWithIdentifier(_:)
    

    改为:

    dequeueReusableCellWithIdentifier(_:forIndexPath:)
    

    您链接到的问题中的 hacky 方法不再有效。此方法返回一个标头。不要将单元格出列。

    您应该使用registerNib(_:forHeaderFooterViewReuseIdentifier:)registerClass(_:forHeaderFooterViewReuseIdentifier:)dequeueReusableHeaderFooterViewWithIdentifier(_:),如文档所述。

    【讨论】:

    • 谢谢!!如果没有你告诉我链接中的方法不再有效,我会浪费很多时间!!我以其他方式实现!
    猜你喜欢
    • 2014-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-17
    • 2012-11-02
    • 1970-01-01
    • 1970-01-01
    • 2018-07-01
    相关资源
    最近更新 更多