【问题标题】:iOS Swift viewForHeaderInSection Not being CallediOS Swift viewForHeaderInSection 未被调用
【发布时间】:2015-05-22 13:54:12
【问题描述】:

我有一个 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"
}

【问题讨论】:

    标签: ios uitableview swift xib


    【解决方案1】:

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

    【讨论】:

    • 是的。你是对的。 tableView.delegate = 自我工作。谢谢你。忘记了。
    • 很高兴这有帮助。不要忘记接受这个答案。谢谢
    • 如果您使用情节提要中的 tableView,那么您可以从情节提要本身设置委托方法。
    【解决方案2】:

    我在 UITableViewController 中没有调用 viewForHeaderInSection 时遇到了这个问题,默认情况下它是委托。原来是这样的

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

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

    【讨论】:

    • 仅适用于任何尝试调试的人:对我来说恰恰相反。没有heightForHeaderInSectionviewForHeaderInSection 不会被调用。
    【解决方案3】:

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

    【讨论】:

    • 我错过了heightForHeaderInSection 方法。如果你只实现视图,它不会被调用。页脚也是如此。
    【解决方案4】:

    在 Swift 3.0 中

    您也可以在 viewDidLoad 中添加 tableView.estimatedSectionHeaderHeight = 40 以调用 viewForHeaderInSection

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-12
      • 2017-02-11
      相关资源
      最近更新 更多