Created
May 31, 2018 13:39
-
-
Save KaneCheshire/9d377dae82caf3e9826cf324e0cd474b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol CellDelegate: class { | |
func cellValueForToggle(_ cell: Cell) -> Bool | |
func cell(_ cell: Cell, didUpdateValueForToggle value: Bool) | |
} | |
class Cell: UITableViewCell { | |
weak var delegate: CellDelegate? | |
override func awakeFromNib() { | |
super.awakeFromNib() | |
let toggle = UISwitch() | |
let isOn = delegate?.cellValueForToggle(self) ?? false | |
toggle.setOn(isOn, animated: false) | |
toggle.addTarget(self, action: #selector(toggleValueChanged(_:)), for: .valueChanged) | |
accessoryView = toggle | |
} | |
@objc private func toggleValueChanged(_ toggle: UISwitch) { | |
delegate?.cell(self, didUpdateValueForToggle: toggle.isOn) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment