gpt4 book ai didi

ios - 为什么重写 tableView( :viewForHeaderInSection:) change the results

转载 作者:行者123 更新时间:2023-11-28 23:41:53 24 4
gpt4 key购买 nike

我有一个没有标题的 TableView 的简单实现。这就是我想要的结果。

class NavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
self.pushViewController(MyTable(style: .grouped), animated: false)
}
}

class MyTable: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return CGFloat.leastNormalMagnitude
}

override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return CGFloat.leastNormalMagnitude
}

override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
assert(false)
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return UITableViewCell()
}
}

table view with no header

请注意,我有一个 viewForHeaderInSection 的覆盖,它永远不会被调用,这是预期的,因为我给出的高度基本上为零。

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
assert(false)
}

当我删除上述函数的定义时,我得到了不同的结果。为什么会发生这种情况?有没有办法在不重写此方法的情况下获得相同的结果?

table view with header

最佳答案

如果 UITableView 的委托(delegate)实现了 tableView(_:viewForHeaderInSection:),则调用 UITableView 会从假定您想要在节标题中使用 标题 转变为认为您想要 views 在您的部分标题中。完成该切换后,它会检查委托(delegate)的 tableView(_:heightForHeaderInSection:) — 并且由于您(有效地)返回零,我猜表格 View 可以在不查询委托(delegate)的情况下进行布局实际查看。

请记住,进行此切换永远不需要调用 view-for-header 方法。 UITableView 可以检查其委托(delegate)是否为该方法实现了 ObjC 选择器,而无需实际调用它,并相应地更改其行为。这很可能就是您的断言永远不会触发的原因。

关于ios - 为什么重写 tableView( :viewForHeaderInSection:) change the results,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53194844/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com