2017-02-17 5 views
5

ObservableとUIPickerViewをバインドする方法はありますか?私はどうなるUITableViewため例えばRxSwiftとUIPickerView

myObservableArray.bindTo(tableView.rx.items(cellIdentifier: "Identifier", cellType: MyCustomTableViewCell.self)) { (row, title, cell) in 
     cell.textLabel?.text = title 
    } 
    .disposed(by: disposeBag) 

UIPickerViewのための同様の何かがありますか?あなたのピッカーのデータソースを提供

+0

使用可能なものは何もRxSwiftのリポジトリからボックスの外にはありません。 'UITableView'バインディングに使われるコードを' UIPickerView'に似たものにすることができますが、RxSwiftでどのように動作しているかを知るにはかなり良い経験が必要です(** I * *試行錯誤) – tomahh

+1

この回答を見てください:https://stackoverflow.com/a/46225009/1953178 –

答えて

0

は次のようになります。

let pickerDataSource: [[String]] = [ ["asdadadad", "sffgddfg"], 
            ["sfsdasgag", "sdfasdfasfsf", "sdsfgagagaggs"] ] 

あなたは、このように必要な「結合」を実装できます。

pickerView.rx.itemSelected.subscribe(onNext: { [weak self] row, component in 
    guard let s = self else { return } 
    s.label.text = s.pickerDataSource[component][row] 
}).addDisposableTo(disposeBag)