Skip to content

Instantly share code, notes, and snippets.

@KaneCheshire
Created May 31, 2018 13:39
Show Gist options
  • Save KaneCheshire/9d377dae82caf3e9826cf324e0cd474b to your computer and use it in GitHub Desktop.
Save KaneCheshire/9d377dae82caf3e9826cf324e0cd474b to your computer and use it in GitHub Desktop.
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