1

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.

Sebastian Dwornik
  • 2,526
  • 2
  • 34
  • 57

3 Answers3

5

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.

Sebastian Dwornik
  • 2,526
  • 2
  • 34
  • 57
1

I had the same issue with the UITableView separators on iPhone X. I was able to solve it changing the cell background:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   let cell: UITableViewCell = ...
   ...

   **cell.backgroundColor = .clear** // <= MAGIC LINE
   return cell
}
-2

You can remove separator by using storyboard and also programatically.

Storyboard

just click on table view and go to attribute inspector from them select separator and change it form default to none .

enter image description here

Code

use below code in viewDidLoad

customtableview.separatorStyle = .none

EDITED HIDE OR REMOVE SEPARATION FROM A SINGLE CELL

Some one already ask the commented question on this community.So here is the solution for handling single cell separation.

MD. Rejaul Hasan
  • 168
  • 1
  • 15
  • Except I want to remove *certain* UITableViewCell separators; not all of them. – Sebastian Dwornik Apr 08 '18 at 19:45
  • @SebastianDwornik I edited my answer for removing a certain UITableViewCell separation. Hope it helps you to solve your problem. Let me know if you have any further question. – MD. Rejaul Hasan Apr 09 '18 at 05:19
  • I've read that solution before, the issue is that shifting the `separatorInset` far off the screen also seems to change the margins on an iPhone X/8plus. Making the text label far left or right justified. :/ – Sebastian Dwornik Apr 09 '18 at 12:07