Last active
September 8, 2021 10:23
-
-
Save arjun011/eccaddd73991bcd97657692ff5181c0e to your computer and use it in GitHub Desktop.
PickerView
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
//How to implement ? | |
let values = self.provinceList.compactMap{$0.name ?? ""} | |
let picker = PickerManager(input: values, selected: values.first) { (value, index) in | |
self.txtProvinces.text = value | |
self.provinceID = self.provinceList[index].provinceID ?? 0 | |
} | |
self.txtProvinces.inputView = picker | |
// Picker Manager | |
class PickerManager<T:Equatable>: UIView, UIPickerViewDelegate, UIPickerViewDataSource { | |
private var inputArray = [T]() | |
private var compilationHandler: ((T,Int) -> Void)? | |
private var inputPickerView = UIPickerView() | |
override init(frame:CGRect) { | |
super.init(frame:frame) | |
} | |
required init?(coder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
init(input:[T], selected:T? ,compilationHandler:@escaping (T,Int) -> Void) { | |
super.init(frame: CGRect.zero) | |
self.autoresizingMask = [.flexibleWidth, .flexibleHeight] | |
//self.inputPickerView = UIPickerView(frame: CGRect.zero) | |
self.inputPickerView.delegate = self | |
self.inputPickerView.dataSource = self | |
self.inputArray = input | |
self.compilationHandler = compilationHandler | |
if let value = selected { | |
inputPickerView.reloadAllComponents() | |
if input.count > 0 { | |
OperationQueue.current?.addOperation { | |
let index = input.firstIndex(of: value) ?? 0 | |
self.inputPickerView.selectRow(index, inComponent: 0, animated: false) | |
} | |
} | |
} | |
self.addSubview(self.inputPickerView) | |
} | |
//MARK: - Picker View deleage and dataSorce methods - | |
func numberOfComponents(in pickerView: UIPickerView) -> Int { | |
return 1 | |
} | |
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { | |
return self.inputArray.count | |
} | |
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { | |
return "\(self.inputArray[row])" | |
} | |
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { | |
if let returnValue = self.compilationHandler { | |
returnValue(self.inputArray[row], row) | |
} | |
} | |
// Only override draw() if you perform custom drawing. | |
// An empty implementation adversely affects performance during animation. | |
override func draw(_ rect: CGRect) { | |
// Drawing code | |
debugPrint(self.frame) | |
debugPrint(self.frame.size) | |
debugPrint(self.frame.origin) | |
self.inputPickerView.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment