fixed picker issue

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2022-12-21 09:31:27 -06:00
parent f60a0978fc
commit 7d0fe4c306

View File

@ -49,6 +49,7 @@ public class PickerSelectorView<EnumType: RawRepresentable>: UIStackView, Picker
public var text: String = "" {
didSet {
label.text = text
updateSelectedIndex()
}
}
public var items: [EnumType] {
@ -64,7 +65,9 @@ public class PickerSelectorView<EnumType: RawRepresentable>: UIStackView, Picker
self.axis = .horizontal
self.distribution = .fillEqually
self.alignment = .fill
text = title
label.text = title
updateSelectedIndex()
addArrangedSubview(label)
addArrangedSubview(button)
button
@ -79,6 +82,11 @@ public class PickerSelectorView<EnumType: RawRepresentable>: UIStackView, Picker
}.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) {
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 {
items.count + 1
items.count
}
public func pickerView( _ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
guard row - 1 >= 0 else { return }
selectedIndex = row
onPickerDidSelect?(items[row-1])
text = "\(items[row-1].rawValue)"
onPickerDidSelect?(items[row])
text = "\(items[row].rawValue)"
pickerView.isHidden = true
}
@ -109,7 +116,7 @@ public class PickerSelectorView<EnumType: RawRepresentable>: UIStackView, Picker
}
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
}