Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove UITableView section header separator

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
    }
like image 929
fragon Avatar asked Feb 17 '17 11:02

fragon


People also ask

How do I remove a separator from a table view?

To hide UITableViewCell separator completely, simply set it's colour to UIColor. clearColor(). This will make the cell separator not visible.

What is grouped Uitableview?

Style. grouped. A table view where sections have distinct groups of rows.


2 Answers

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
}
like image 63
Nirav D Avatar answered Nov 16 '22 00:11

Nirav D


Try this:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView = UIView()
    headerView.backgroundColor = UIColor.clear
    return headerView
}
like image 21
iParesh Avatar answered Nov 16 '22 00:11

iParesh