iosswiftuitableviewseparatorsubviews

Why isn't UITableViewCell's Separator View listed?


I want to remove certain UITableViewCell separators (not all of them).

I see them in the UI, but don't understand why they don't get listed when I print out the subview hierarchy?

for view in cell.subviews {
    DDLogDebug(String(describing: type(of: view)))
}

enter image description here

This seems to happen upon initial view. If I scroll the UITableView cells far out of view and then back again, the UITableViewCell's separator magically appears in the subview log printout. What's going on here?

note: I've only tested on the simulator as I don't own an iPhone X currently.


Solution

  • Working with UITableViewCell's outside of the cell doesn't seem as reliable as simply subclassing the cell and taking control from within it.

    Hence my solution below:

    class myCell: UITableViewCell {
    
        var withSeparator: Bool = true
    
        func layoutSubviews() {
            super.layoutSubviews()
    
            if !withSeparator {
                self.removeBottomSeparator()
            }
    
        }
    ...
    
    
    
    extension UITableViewCell {
        func removeBottomSeparator() {
            // Remove it if it's found.
            for view in self.subviews where String(describing: type(of: view)).hasSuffix("SeparatorView") {
                view.removeFromSuperview()
            }
    
        }
    }
    

    Using this method allows me to find the SeparatorView every time and be able to remove it. 👍