iOS Swift viewForHeaderInSection не вызывается
У меня есть UITableview я пытаюсь добавить HeaderView
на мой UITableview
.
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"
}
4 ответа:
Метод
viewForHeaderInSection
принадлежит протоколуUITableViewDelegate
. Поэтому вы должны установитьdelegate
Вашего табличного представления на контроллер представления, чтобы получить этот обратный вызов. Вы, вероятно, просто установили tableviewdataSource
, Как называются другие ваши методы.
В 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 }
В Swift 3.0
Вы также можете добавить tableView.estimatedSectionHeaderHeight = 40 в viewDid, чтобы получить viewForHeaderInSection называется
У меня была эта проблема с viewForHeaderInSection не вызывается в UITableViewController, который является делегатом по умолчанию. Оказалось, что
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
Препятствовал вызову viewForHeaderInSection. Таким образом, если делегат не работает, вам может потребоваться проверить, что вы не переопределяете один из других методов заголовка раздела.