Hello! There is a tableView , there is a UISwitch on it. Can I click on a cell to change the state of UISwitch?

Sorry for this question. I'm still new to iOS development :)

    1 answer 1

    Yes you can. Use the method: func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) . This method is called when you click on a cell, determine the cell type: guard let cell = tableView.cellForRow(at: indexPath) as? YourCellType else { fatalError("Unexpected cell type") } guard let cell = tableView.cellForRow(at: indexPath) as? YourCellType else { fatalError("Unexpected cell type") } and then switch UISwitch : cell.switch.setON = true/false

    This is how the code will look like:

     override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.deselectRow(at: indexPath, animated: true) guard let cell = tableView.cellForRow(at: indexPath) as? YourCellType else { fatalError("Unexpected cell type") } cell.switch.setOn = !cell.switch.isOn }