fixed picker issue
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
f60a0978fc
commit
7d0fe4c306
@ -49,6 +49,7 @@ public class PickerSelectorView<EnumType: RawRepresentable>: UIStackView, Picker
|
|||||||
public var text: String = "" {
|
public var text: String = "" {
|
||||||
didSet {
|
didSet {
|
||||||
label.text = text
|
label.text = text
|
||||||
|
updateSelectedIndex()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public var items: [EnumType] {
|
public var items: [EnumType] {
|
||||||
@ -64,7 +65,9 @@ public class PickerSelectorView<EnumType: RawRepresentable>: UIStackView, Picker
|
|||||||
self.axis = .horizontal
|
self.axis = .horizontal
|
||||||
self.distribution = .fillEqually
|
self.distribution = .fillEqually
|
||||||
self.alignment = .fill
|
self.alignment = .fill
|
||||||
|
text = title
|
||||||
label.text = title
|
label.text = title
|
||||||
|
updateSelectedIndex()
|
||||||
addArrangedSubview(label)
|
addArrangedSubview(label)
|
||||||
addArrangedSubview(button)
|
addArrangedSubview(button)
|
||||||
button
|
button
|
||||||
@ -79,6 +82,11 @@ public class PickerSelectorView<EnumType: RawRepresentable>: UIStackView, Picker
|
|||||||
}.store(in: &subscribers)
|
}.store(in: &subscribers)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func updateSelectedIndex() {
|
||||||
|
guard let foundTextIndex = items.firstIndex(where: { "\($0.rawValue)" == text }) else { return }
|
||||||
|
selectedIndex = foundTextIndex //add one since we always have a empty space
|
||||||
|
}
|
||||||
|
|
||||||
public required init(coder: NSCoder) {
|
public required init(coder: NSCoder) {
|
||||||
fatalError("init(coder:) has not been implemented")
|
fatalError("init(coder:) has not been implemented")
|
||||||
}
|
}
|
||||||
@ -88,14 +96,13 @@ public class PickerSelectorView<EnumType: RawRepresentable>: UIStackView, Picker
|
|||||||
}
|
}
|
||||||
|
|
||||||
public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
|
public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
|
||||||
items.count + 1
|
items.count
|
||||||
}
|
}
|
||||||
|
|
||||||
public func pickerView( _ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
|
public func pickerView( _ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
|
||||||
guard row - 1 >= 0 else { return }
|
|
||||||
selectedIndex = row
|
selectedIndex = row
|
||||||
onPickerDidSelect?(items[row-1])
|
onPickerDidSelect?(items[row])
|
||||||
text = "\(items[row-1].rawValue)"
|
text = "\(items[row].rawValue)"
|
||||||
pickerView.isHidden = true
|
pickerView.isHidden = true
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -109,7 +116,7 @@ public class PickerSelectorView<EnumType: RawRepresentable>: UIStackView, Picker
|
|||||||
}
|
}
|
||||||
|
|
||||||
private func title(for row: Int) -> String {
|
private func title(for row: Int) -> String {
|
||||||
guard row > 0, let item = items[row-1].rawValue as? String else { return "" }
|
guard let item = items[row].rawValue as? String else { return "" }
|
||||||
return item
|
return item
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user