UIPickerView的使用

需要引入 RxDataSources ,它提供了许多 pickerView 适配器

适配器类型:RxPickerViewStringAdapter(普通文本)、RxPickerViewAttributedStringAdapter(富文本)、RxPickerViewViewAdapter(自定义)

下方代码以RxPickerViewStringAdapter为例

  • 单列适配器

private let stringPickerAdapter = RxPickerViewStringAdapter<[String]>(
    components: [],
    numberOfComponents: { _,_,_  in 1 },
    numberOfRowsInComponent: { (_, _, items, _) -> Int in
        return items.count},
    titleForRow: { (_, _, items, row, _) -> String? in
        return items[row]}
  )
  • 多列适配器

private let stringPickerAdapter = RxPickerViewStringAdapter<[[String]]>(
    components: [],
    numberOfComponents: { dataSource,pickerView,components  in components.count },
    numberOfRowsInComponent: { (_, _, components, component) -> Int in
        return components[component].count},
    titleForRow: { (_, _, components, row, component) -> String? in
        return components[component][row]}
)
  • 绑定pickerView数据

Observable.just(["One", "Two", "Tree"])
    .bind(to: pickerView.rx.items(adapter: stringPickerAdapter))
    .disposed(by: disposeBag)
  • 选中方法

pickerView.rx.itemSelected
pickerView.rx.modelSelected   // 这个还不太会用

参考文章:Swift - RxSwift的使用详解41(UIPickerView的使用)

你可能感兴趣的:(UIPickerView的使用)