Change the background color of the "header" of a UITableView

1

I have the following table

@IBOutlet weak var menuTableView: UITableView!

Which I complete with the following code

override func viewWillAppear(_ animated: Bool) {
    menuTableView.reloadData()
}

// MARK: - Table view data source
func numberOfSections(in tableView: UITableView) -> Int {
    return 6
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 15
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let mainCell = Bundle.main.loadNibNamed("PanelHomeTableViewCell", owner: self, options: nil)?.first as! PanelHomeTableViewCell
    mainCell.selectionStyle = .none
    mainCell.backgroundColor = Utils.Color.COLOR_E8E8E8

    mainCell.strLblTitleOptionsMenu.textColor = Utils.Color.COLOR_2B5034
    switch (indexPath.section)
    {
    case 0:
        mainCell.strLblTitleOptionsMenu.text = NSLocalizedString("strLblOption1",comment:"")
        mainCell.imgOptionsMenu.image = UIImage.init(named:"PagarMenu")
        return mainCell
    case 1:
        mainCell.strLblTitleOptionsMenu.text = NSLocalizedString("strLblOption2",comment:"")
        mainCell.imgOptionsMenu.image = UIImage.init(named:"BillingMenu")
        return mainCell
    case 2:
        mainCell.strLblTitleOptionsMenu.text = NSLocalizedString("strLblOption3",comment:"")
        mainCell.imgOptionsMenu.image = UIImage.init(named:"CollectionNotificationsMenu")
        return mainCell
    case 3:
        mainCell.strLblTitleOptionsMenu.text = NSLocalizedString("strLblOption4",comment:"")
        mainCell.imgOptionsMenu.image = UIImage.init(named:"billingQueryMenu")
        return mainCell
    case 4:
        mainCell.strLblTitleOptionsMenu.text = NSLocalizedString("strLblOption5",comment:"")
        mainCell.imgOptionsMenu.image = UIImage.init(named:"MovsBalanceMenu")
        return mainCell
    case 5:
        mainCell.strLblTitleOptionsMenu.text = NSLocalizedString("strLblOption6",comment:"")
        mainCell.imgOptionsMenu.image = UIImage.init(named:"CashOutMenu")
        return mainCell
    default:
        break
    }
    return mainCell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 40
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    switch (indexPath.section)
    {
    case 0:
        self.presenter.postPayment()
        break
    case 1:
        self.presenter.collectPayment()
        break
    case 2 :
        self.presenter.queryNotifications()
        break
    case 3:
        self.presenter.queryCollection()
        break
    case 4:
        self.presenter.getMovements()
        break
    case 5:
        self.presenter.atmWithdrawal()
        break
    default:
        break
    }
    tableView.deselectRow(at: indexPath, animated: true)
}   

Which leaves an empty header of high 15, but the bottom of my screen is white, and this header is like gray. I would like to be able to put it blank, it can be a Swift code or directly from the visual tools.

Thank you.

    
asked by Bruno Sosa Fast Tag 18.04.2018 в 15:27
source

1 answer

3

Using the tableView(_:viewForHeaderInSection:) method of your UITableViewDelegate you can configure the header as you like.

See documentation .

It would be something like this:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let viewFrame = CGRect(x: 0, y: 0, width: tableView.bounds.width, height: 15)
    var headerView = UIView(frame: viewFrame)
    headerView.backgroundColor = UIColor.white
    return headerView
}
    
answered by 18.04.2018 / 15:32
source