dequeueReusableCellWithIdentifier 不断返回 nil [英] dequeueReusableCellWithIdentifier keeps returning nil
问题描述
我基本上是在尝试执行此链接中的操作
<块引用><块引用>在我的故事板中,我在 TableViewController 中嵌入了静态单元
我选择了一个表格视图单元格并将标识符设置为CustomHeader"(在故事板中)
以下是我的代码片段
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) ->界面视图?{var headerView:UITableViewCell?= tableView.dequeueReusableCellWithIdentifier("CustomHeader") as?UITableViewCellif (headerView == nil){println("errrrrrr")}返回标题视图}覆盖 func tableView(tableView: UITableView, heightForHeaderInSection 部分: Int) ->CGFloat {返回 30}
但是,控制台不断打印errrrrrr",表示 headerView 为零.
但我认为标识符为CustomHeader"的不能为零,因为我手动选择了一个单元格并将其标识符设置为故事板中的CustomHeader!!
我是 iOS 新手,所以我不明白问题是什么.
感谢任何帮助.
提前致谢!
要将单元格从故事板中出列,您需要使用采用索引路径的方法.也就是说,不要使用:
dequeueReusableCellWithIdentifier(_:)
改为使用:
dequeueReusableCellWithIdentifier(_:forIndexPath:)
您链接的问题中的hacky方法不再有效.此方法返回一个标题.不要让单元格出列.
您应该使用 registerNib(_:forHeaderFooterViewReuseIdentifier:)
或 registerClass(_:forHeaderFooterViewReuseIdentifier:)
和 dequeueReusableHeaderFooterViewWithIdentifier(_:)
,如文档所述.
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!
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.
这篇关于dequeueReusableCellWithIdentifier 不断返回 nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!