Rx で Pickerview を使用しています。選択した行のビューをカスタム ビューで変更する方法を知りたいです。RXなしでピッカービューを試してみましたが、動作します。しかし、Rx とカスタム アダプターの場合も同様に機能しません。これを行うアイデアはありますか?
ご回答ありがとうございます :
たとえば、 Rxswift を使用しない場合:
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
let customView = LimitItemPickerView(frame: CGRect.zero)
if pickerView.selectedRow(inComponent: component) == row {
customView.selected()
}
return customView
}
私の実装:
final class `PickerViewViewAdapter: NSObject, UIPickerViewDataSource, UIPickerViewDelegate, RxPickerViewDataSourceType, SectionedViewDataSourceType {
typealias Element = [MyMODEL]
private var items: [MyMODEL] = []
func model(at indexPath: IndexPath) throws -> Any {
return items[indexPath.row]
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return items.count
}
func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
return 80.0
}
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
let customView = MyCustomView(frame: CGRect.zero, model: items[row])
return customView
}
func pickerView(_ pickerView: UIPickerView, observedEvent: Event<Element>) {
UIBindingObserver(UIElement: self) { (adapter, items) in
adapter.items = items
pickerView.reloadAllComponents()
}.on(observedEvent)
}
}`
これを使用するには:
let input = MYVIEWMODEL.Input(param1: key,
param2: key2,
param3: key3)
let output = myViewModel.transform(input: input)
output.limitsAvailable
.drive(limitPickerView.rx.items(adapter: PickerViewViewAdapter()))
.disposed(by: disposeBag)