2017-02-17 65 views
5

有没有办法将UIPickerView与Observable绑定?RxSwift和UIPickerView

例如,对于一个UITableView我会做:

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)