iOS Swift viewForHeaderInSection Not being Called

27,775

Solution 1

The viewForHeaderInSection method belongs to the UITableViewDelegate Protocol. You therefore must set the delegate of your table view to your view controller in order to get this callback. You probably did just set the tableview's dataSource as your other methods are called.

Solution 2

In swift 3

You should call

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
    }

Solution 3

in Swift 3.0

you can also add tableView.estimatedSectionHeaderHeight = 40 into the viewDidLoad to get viewForHeaderInSection called

Share:
27,775
user3110353
Author by

user3110353

Updated on July 05, 2022

Comments

  • user3110353
    user3110353 almost 2 years

    I have a UITableview I am trying to add a HeaderView onto my UITableview .

    However the viewForHeaderInSection is not being called, but I see the titleForHeaderInSection is being called or is shown. I also tried using a custom cell header for this and it also does not work.

    I have no idea what I am doing wrong.

    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

  • user3110353
    user3110353 over 9 years
    Yes. You are correct. tableView.delegate = self works. Thank you. forgot about that .
  • croX
    croX over 9 years
    Glad this helped. Don't forget to accept this answer. Thank you
  • iDevAmit
    iDevAmit almost 8 years
    If you uses the tableView from storyboard then you can set the delegate method from storyboard itself.
  • djv
    djv over 7 years
    Just for anyone trying to debug: for me it was the opposite. viewForHeaderInSection wasn't getting called without heightForHeaderInSection.
  • olivaresF
    olivaresF about 7 years
    I was missing the heightForHeaderInSection method. If you implement only view, it won't get called. Same thing for footers.