IOS Swift viewForHeaderInSection Не вызывается

У меня есть 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"
}

enter image description here

Ответы

Ответ 1

Метод viewForHeaderInSection принадлежит протоколу UITableViewDelegate. Поэтому для получения этого обратного вызова вы должны установить delegate вашего представления таблицы на свой контроллер представления. Вероятно, вы просто установили tableview dataSource, когда вызываются другие ваши методы.

Ответ 2

В быстро 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
    }

Ответ 3

в Swift 3.0

вы также можете добавить tableView.estimatedSectionHeaderHeight = 40 в viewDid, чтобы получить viewForHeaderInSection, называемый

Ответ 4

У меня была эта проблема, поскольку viewForHeaderInSection не вызывается в UITableViewController, который по умолчанию является делегатом. Оказалось, что

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

препятствует вызову viewForHeaderInSection. Таким образом, если делегирование не работает, вам может потребоваться проверить, что вы не переопределяете один из методов заголовка другого раздела.