I have the following custom UITableViewCell
:
I want my view controller to be notified when somebody flips a cell's switch in order to update my model. I've tried using the table view's delegate methods (didSelect
, didFinishEditing
, didHighlight
, etc.) but none of them are called upon this action. Is there any way to do what I want to do? Somebody please help.
actually your UISwitch has added to accessoryView
of UITableViewCell
, so do like on cellforRowAtIndex
var switchView = UISwitch(frame: CGRect.zero)
aCell.accessoryView = switchView
lightSwitch.tag = indexPath.row
switchView.setOn(false, animated: false)
switchView.addTarget(self, action: #selector(switchChanged(_:), for: .valueChanged)
and get the action of UISwitch
as
func switchChanged(_ sender: UISwitch) {
print("which switch is \(sender.tag)")
print("The switch is \(sender?.on ? "ON" : "OFF")")
}