Skip to content

Instantly share code, notes, and snippets.

@s1ddok
Last active May 29, 2018 11:19
Show Gist options
  • Save s1ddok/80cec6f9e7ce1fd7f681bee0c8c2c438 to your computer and use it in GitHub Desktop.
Save s1ddok/80cec6f9e7ce1fd7f681bee0c8c2c438 to your computer and use it in GitHub Desktop.
dct-snippet3
// Declare model class for the option
public class BoolOption: Option {
public typealias Handler = (Bool) -> Void
public var name: String
public internal(set) var value: Bool {
didSet {
self.onChangeHandler?(self.value)
}
}
public var onChangeHandler: Handler?
public init(name: String, initialValue: Bool, onChangeHandler: @escaping Handler) {
self.name = name
self.value = initialValue
self.onChangeHandler = onChangeHandler
}
}
// Declare UITableViewCell subclass in a different file for its representation
import UIKit.UITableViewCell
class BoolOptionCell: UITableViewCell, OptionCell {
@IBOutlet weak var optionName: UILabel!
@IBOutlet weak var `switch`: UISwitch!
internal var option: BoolOption?
@IBAction func optionDidChange(_ sender: UISwitch) {
self.option?.value = sender.isOn
}
public func configure(for option: Option) {
guard let boolOption = option as? BoolOption else {
fatalError("Wrong option was used for configuring the cell")
}
self.optionName.text = option.name
self.switch.isOn = boolOption.value
self.option = boolOption
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment