我有一个UITableview我试图在UITableview
上添加HeaderView
。iOS Swift viewForHeaderInSection未被调用
但是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"
}
是的。你是对的。 tableView.delegate =自我工作。谢谢。忘了那个。 – user3110353 2015-03-19 12:44:31
很高兴这有帮助。不要忘记接受这个答案。谢谢 – croX 2015-03-19 13:14:22
如果使用storyboard中的tableView,则可以从storyboard本身设置委托方法。 – iDevAmit 2016-09-27 03:10:55