3

我将 Pickerview 与 Rx 一起使用,我想知道如何使用自定义视图更改所选行的视图。我尝试了没有 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)
4

0 回答 0