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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 05:16:13  来源:igfitidea点击:

iOS Swift viewForHeaderInSection Not being Called

iosuitableviewswiftxib

提问by user3110353

I have a UITableview I am trying to add a HeaderViewonto 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"
}

enter image description here

在此处输入图片说明

回答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 viewForHeaderInSectionmethod belongs to the UITableViewDelegateProtocol. You therefore must set the delegateof your table view to your view controller in order to get this callback. You probably did just set the tableview's dataSourceas 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。因此,如果委托不起作用,您可能需要检查您是否没有覆盖其他部分标题方法之一。