Trying to change the background color for a tableView Cell

I am Trying to change the background color for a tableView Cell when selected with the following code, however it does not work.

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        nameTextField.text = contacts[indexPath.row].name
        phoneTextField.text = contacts[indexPath.row].phone
        addressTextField.text = contacts[indexPath.row].address
        selectedContact = indexPath.row
       let cell = tableView.dequeueReusableCell(withIdentifier: "ContactCell", for: indexPath)
        let bgColorView = UIView()
        bgColorView.backgroundColor = UIColor.green
        cell.selectedBackgroundView = bgColorView

What is wrong with this code?
I don’t think dequeueReusableCell is the right call. That is meant to be used when setting up a new cell, not for getting a reference to an existing one. You could try cellForRow(at:).

Or better yet, just set up the selected background view when you first create the cell. Then you wouldn’t have to do anything when the cell is selected. The table view should do it for you automatically.
Trying to change the background color for a tableView Cell
 
 
Q