繁体   English   中英

iOS Swift viewForHeaderInSection 未被调用

[英]iOS Swift viewForHeaderInSection Not being Called

我有一个 UITableview 我想在我的HeaderView上添加一个UITableview

但是 viewForHeaderInSection 没有被调用,但我看到 titleForHeaderInSection 正在被调用或显示。 我也尝试为此使用自定义单元格标题,但它也不起作用。

我不知道我做错了什么。

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"
}

在此处输入图片说明

迅捷 3

你应该打电话

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
    }

viewForHeaderInSection方法属于UITableViewDelegate协议。 因此,您必须将表视图的delegate设置为视图控制器才能获得此回调。 您可能只是在调用其他方法时设置了 tableview 的dataSource

在 Swift 3.0 中

您还可以添加 tableView。 estimatedSectionHeaderHeight = 40到viewDidLoad中得到viewForHeaderInSection

我在 UITableViewController 中没有调用 viewForHeaderInSection 时遇到了这个问题,默认情况下这是委托。 事实证明

override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat

正在阻止调用 viewForHeaderInSection。 因此,如果委托不起作用,您可能需要检查您是否没有覆盖其他部分标题方法之一。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM