Last active
May 29, 2018 11:19
-
-
Save s1ddok/80cec6f9e7ce1fd7f681bee0c8c2c438 to your computer and use it in GitHub Desktop.
dct-snippet3
This file contains 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
// 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