Created
December 11, 2019 11:12
-
-
Save 5SMNOONMS5/deb348adec6e1ad51d93d4c03f9d6bf8 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 CLSPickerViewProtocol { | |
var title: String { get } | |
var id: Int { get } | |
} | |
final class CLSPickerView<T: LLPickerViewProtocol>: UIPickerView, UIPickerViewDelegate, UIPickerViewDataSource { | |
var selectHandler: ((T) -> Void)? | |
var contents: [T] = [] { | |
didSet { | |
DispatchQueue.main.async { | |
self.reloadAllComponents() | |
} | |
} | |
} | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
setup() | |
} | |
required init?(coder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
private func setup() -> Void { | |
super.dataSource = self | |
super.delegate = self | |
} | |
func numberOfComponents(in pickerView: UIPickerView) -> Int { | |
return 1 | |
} | |
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { | |
return contents.count | |
} | |
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { | |
return contents[row].title | |
} | |
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { | |
selectHandler?(contents[row]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment