ios - dequeueReusableCellWithIdentifier 一直返回 nil

标签 ios uitableview swift

我基本上是在尝试执行此链接中的操作

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

在我的 Storyboard中,我在 TableViewController 中嵌入了静态单元格

我选择了一个表格 View 单元格并将标识符设置为“CustomHeader”(在 Storyboard中)

下面是我的代码片段

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,因为我手动选择了一个单元格并在 Storyboard中将其标识符设置为CustomHeader!!

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

感谢任何帮助。

提前致谢!

最佳答案

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

dequeueReusableCellWithIdentifier(_:)

相反,使用:

dequeueReusableCellWithIdentifier(_:forIndexPath:)

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

您应该使用 registerNib(_:forHeaderFooterViewReuseIdentifier:)registerClass(_:forHeaderFooterViewReuseIdentifier:),以及 dequeueReusableHeaderFooterViewWithIdentifier(_:),如文件所示。

关于ios - dequeueReusableCellWithIdentifier 一直返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27843496/

相关文章:

iphone - iPhone应用程序存储量不断增长

ios - 创建包含常用方法的 `Core` 类

ios - 在 UITableViewCell 中获取正确的初始布局

ios - 我如何在 UIAlertController 中获取 textField 的值?

ios - 在 TableView 委托(delegate)方法中发出异步请求

ios - 仅针对选定/突出显示的值显示圆圈

ios - 解析使用 Auth0 认证的用户

ios - 每 24 小时在 uitableview 上创建时间戳 iOS,如 WhatsAPP 或 Viber

swift - 如何在 UITableViewCell 中创建一个圆形?

Swift;将自定义 View Controller 转换到 View Controller