Skip to content

Instantly share code, notes, and snippets.

@5SMNOONMS5
Created December 11, 2019 11:12
Show Gist options
  • Save 5SMNOONMS5/deb348adec6e1ad51d93d4c03f9d6bf8 to your computer and use it in GitHub Desktop.
Save 5SMNOONMS5/deb348adec6e1ad51d93d4c03f9d6bf8 to your computer and use it in GitHub Desktop.
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