UITableView - Custom selectedBackgroundView in custom UITableViewCell hides cell separator when selected

Viewed 706

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.

1 Answers
Related