iOS Swift viewForHeaderInSection 未被调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29144793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS Swift viewForHeaderInSection Not being Called
提问by user3110353
I have a UITableview I am trying to add a HeaderView
onto my UITableview
.
我有一个 UITableview 我想HeaderView
在我的UITableview
.
However the viewForHeaderInSection is not being called, but I see the titleForHeaderInSection is being called or is shown. I also tried using a custom cell header for this and it also does not work.
但是 viewForHeaderInSection 没有被调用,但我看到 titleForHeaderInSection 正在被调用或显示。我也尝试为此使用自定义单元格标题,但它也不起作用。
I have no idea what I am doing wrong.
我不知道我做错了什么。
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Groceries"
tableView.registerNib(UINib(nibName: "TransactionSpecifiedCategoriesTableViewCell", bundle: nil), forCellReuseIdentifier: "TransactionSpecifiedCategoriesTableViewCell")
tableView.registerNib(UINib(nibName: "TransactionSpecifiedCategoriesHeaderViewCell", bundle: nil), forHeaderFooterViewReuseIdentifier: "TransactionSpecifiedCategoriesHeaderViewCell")
tableView.tableFooterView = UIView(frame: CGRectMake(0, 0, 0, 0))
}
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let footerView = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 100))
footerView.backgroundColor = UIColor.blackColor()
return footerView
//let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView
//var headerView = UIView(frame: CGRectMake(0, 0, 100, 320))
//headerView.backgroundColor = UIColor.blackColor()
//
//return headerView
}
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 200.0
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "First section header title"
}
回答by karthikeyan
In swift 3
在迅捷 3
You should call
你应该打电话
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let rect = CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 44)
let footerView = UIView(frame:rect)
footerView.backgroundColor = UIColor.clear
return footerView
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 44
}
回答by croX
The viewForHeaderInSection
method belongs to the UITableViewDelegate
Protocol. You therefore must set the delegate
of your table view to your view controller in order to get this callback. You probably did just set the tableview's dataSource
as your other methods are called.
该viewForHeaderInSection
方法属于UITableViewDelegate
协议。因此delegate
,您必须将表视图的 设置为您的视图控制器才能获得此回调。您可能只是dataSource
在调用其他方法时设置了 tableview 。
回答by Craig P
in Swift 3.0
在 Swift 3.0 中
you can also add tableView.estimatedSectionHeaderHeight = 40 into the viewDid to get viewForHeaderInSection called
您还可以将 tableView.estimatedSectionHeaderHeight = 40 添加到 viewDid 中以获取调用的 viewForHeaderInSection
回答by Damien Del Russo
I had this issue with viewForHeaderInSection not being called in the UITableViewController, which is the delegate by default. It turned out that
我在 UITableViewController 中没有调用 viewForHeaderInSection 时遇到了这个问题,默认情况下这是委托。事实证明
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
was preventing viewForHeaderInSection from being called. Thus, if the delegate thing doesn't work, you may need to check that you aren't overriding one of the other section header methods.
正在阻止调用 viewForHeaderInSection。因此,如果委托不起作用,您可能需要检查您是否没有覆盖其他部分标题方法之一。