[英]tableView(_:viewforHeaderInSection:) Not Being Called
这个让我发疯。
UITableViewController
子类。UITableViewController
既是其表视图的德尔门也是数据源)。tableView(_:titleForHeaderInSection:)
视图控制器(委托)实现:
tableView(_:viewforHeaderInSection:)
tableView(_:heightForHeaderInSection:)
tableView(_:estimatedHeightForHeaderInSection)
...两者都没有被调用过。
因此,我的表格视图部分不显示任何标题。
如果我指定表视图的全局部分标题高度:
override func viewDidLoad() {
super.viewDidLoad()
tableView.estimatedSectionHeaderHeight = 12 // (say)
...然后显示空白标题,但tableView(_:titleForHeaderInSection:)
等仍然没有被调用,我的自定义标题视图永远不会显示。
以前也有人问过类似的问题,Apple 的文档似乎不是 100% 正确的,但是我很确定我正在做所需的一切(我已经尝试了所有配置)。
我错过了什么?
代码库很大,不是我的; 也许我在这里错过了一些东西,但不知道还要寻找什么......
更新
我创建了一个新的最小项目来测试,结果我只需要实现这两个委托方法(而不是修改表视图的任何属性):
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) // (say)
view.backgroundColor = UIColor.red // (say)
return view
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 10.0 // (say)
}
...显示我的自定义部分标题视图。
但是,相同的设置不适用于我现有的项目。 有什么东西,一定有什么地方干扰了……
更新 2
我 sublcassed UITableView
以查看发生了什么,但仍然无法弄清楚(请参阅内联注释):
导入 UIKit
class DebugTableView: UITableView {
// GETS EXECUTED:
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override var estimatedSectionHeaderHeight: CGFloat {
// NEVER GETS EXECUTED:
get {
return super.estimatedSectionHeaderHeight
}
// NEVER GETS EXECUTED:
set (newValue) {
super.estimatedSectionHeaderHeight = newValue
}
}
override var sectionHeaderHeight: CGFloat {
// NEVER GETS EXECUTED:
get {
return super.sectionHeaderHeight
}
// NEVER GETS EXECUTED:
set (newValue) {
super.sectionHeaderHeight = newValue
}
}
// NEVER GETS EXECUTED:
override func headerView(forSection section: Int) -> UITableViewHeaderFooterView? {
let view = super.headerView(forSection: section)
return view
}
// GETS EXECUTED ONCE PER CELL:
override func rectForHeader(inSection section: Int) -> CGRect {
var rect = super.rectForHeader(inSection: section)
rect.size.height = 1000 // HAS NO EFFECT
return rect
}
}
您只需要设置sectionHeaderHeight
,而不仅仅是估计高度!
喜欢,
斯威夫特 4.0
tableView.sectionHeaderHeight = UITableView.automaticDimension
tableView.estimatedSectionHeaderHeight = 25
然后只需覆盖viewForHeaderInSection
。 并且无需覆盖任何其他代表!
我发现了问题。
事实证明,有一个辅助对象取代了表视图委托的角色并将其隐藏。
这节课:
UITableViewDelegate
协议并实现其大部分方法(但不包括与页眉/页脚相关的方法),viewDidAppear()
方法中将自身设置为表视图的委托,当它的实现(比如)tableView(_:didSelectRowAtIndexPath:) 被调用时,它依次调用表视图控制器的等效方法,给人的印象是视图控制器仍然是委托:
class ManInTheMiddle: NSObject, UITableViewDelegate { var formerDelegate: UITableViewController? func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { formerDelegate?.tableView(tableView, didSelectRowAt: indexPath) // Do some other stuff... } }
这就是为什么UITableViewDelegate
某些方法在我的表视图控制器上被调用而有些方法没有被调用的原因。
我通过覆盖表视图的delegate
属性发现了这一点:
class DebugTableView: UITableView {
// ...
override var delegate: UITableViewDelegate? {
get {
return super.delegate
}
set (newValue) {
super.delegate = newValue // < BREAKPOINT HERE
}
}
...出于绝望(没有充分的理由怀疑委托被交换,因为大多数方法都有效......)
<rant>该死的,我讨厌这些肮脏的黑客吗...这几乎是#define true false
领域,sheesh... </rant>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.