Andrew
Andrew

Reputation: 7883

Hiding UITableViewCell separator for individual cells

I require a UITableViewCell separator for the majority of my cells, but some of them should be hidden.

The way this is set is self.tableView.separatorStyle, which would apply to every cell.

A heavy-handed work around is to disable it, and then draw it onto cells manually as a UIView, but I don't want to go there.

How can I otherwise remove the separator for individual cells?

Upvotes: 1

Views: 1193

Answers (3)

Dave Batton
Dave Batton

Reputation: 8845

Adding a custom view as a separator is a lot of work. Besides adding a property, setting constraints, etc, you need to worry about matching the color and line width of the default separators (1.0 / view.window.screen.scale so it's one pixel tall, not one point).

It's way easier to use the default separator view, and hide it by setting the insets. For example, turn on separators for the table, then in your UITableViewCell subclass do something like this:

var showsSeperator: Bool = true {
    didSet {
        let leftInset: CGFloat
        if showsSeperator {
            leftInset = layoutMargins.left
        } else {
            leftInset = 1000  // Out of site.
        }
        separatorInset = UIEdgeInsets(top: 0, left: leftInset, bottom: 0, right: 0)
    }
}

Upvotes: 0

user7388
user7388

Reputation: 1741

You can have custom cell and add separator in this cell, this way you can manage your cell separator. Or you can addSubView a UIView for separator to your cell like iPatel's answer, but be careful for memory issue.

Upvotes: 1

kaar3k
kaar3k

Reputation: 994

Use a different ReUseIdentifier for the cells which you don need separatorStyle.Don draw anything onto this cell.

Upvotes: 0

Related Questions