How to remove UITableView section header separator

12,440

Solution 1

Don't know why you are returning UITableViewCell in viewForHeaderInSection method so may be it is possible that is showing separator of that UITableViewCell. Try returning the contentView of cell instead of cell from viewForHeaderInSection method.

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let cell = tableView.dequeueReusableCell(withIdentifier: "headerTableViewCell")
    return cell.contentView
}

Solution 2

Try this:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView = UIView()
    headerView.backgroundColor = UIColor.clear
    return headerView
}
Share:
12,440

Related videos on Youtube

fragon
Author by

fragon

Updated on July 13, 2022

Comments

  • fragon
    fragon almost 2 years

    I would like to remove (or make them clearColor) UITableView's section header separators. Setting tableView.separatorStyle = .none doesn't work. I've also tried solutions from here, but none of them actually worked for me (maybe because the answers are pretty old). I still get this tiny separator below the section header.

    I created the UITableView in Storyboard and add a UITableViewCell there. Then I set it as a header like this:

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
            let cell = tableView.dequeueReusableCell(withIdentifier: "headerTableViewCell")
            return cell
        }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
            if section == 1 {
                return 49
            }
            return 0
        }
    
    • Nitin Gohel
      Nitin Gohel about 7 years
      why you add cell in viewForHeaderInSection ?
  • Jin Wang
    Jin Wang over 6 years
    Actually this is a great way. Although you can just do return 0.
  • inexcitus
    inexcitus almost 5 years
    Under some circumstances, 0 is ignored (Grouped View). You should return CGFloat.leastNormalMagnitude instead.