4

I have a UITableView that renders cells of the custom class DrawerTableCell, that is declared this way:

class DrawerTableCell: UITableViewCell {

    @IBInspectable var selectionColor: UIColor = .gray {
        didSet {
            configureSelectedBackgroundView()
        }
    }

    func configureSelectedBackgroundView() {
        let view = UIView()
        view.backgroundColor = selectionColor
        selectedBackgroundView = view
    }
}

This UITableViewCell child class was created in order to be able to specify the selected background color for the cell.

In my View Controller I set:

tableView.separatorColor = .white

and I configure the cell this way:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "drawerCell") as! DrawerTableCell
    cell.selectionColor = UIColor.blue
    return cell
}

The results is that when there is no cell selected, all the separators display correctly. When I select a cell, the background color is showed, but the separator is hidden only in the selected cell.

Any ideas on how to keep the separator line in the selected cell?

Thanks.

gsobrevilla
  • 243
  • 2
  • 12

1 Answers1

1

according to Apple's documentation, UITableViewCell adds the value of this property as a subview only when the cell is selected. It adds the selected background view as a subview directly above the background view (backgroundView) if it is not nil, or behind all other views.

So according to this, I think your separator is hidden because it's underneath your new view. Try setting your selectionColor to .clear and see what happens. If this doesn't work, you can also try removing separators on your table view completely, and just add a 0.5px line to the bottom of each cell instead.

cloudcal
  • 505
  • 1
  • 4
  • 9