How to implement UIPickerView using RXSwift

like UITableView

items.bindTo(tableView.rx.items(cellIdentifier: "cellIdentifier", cellType: AttentionTableViewCell.self)){(row,dic,cell) in cell.configueCell(with: dic) }.addDisposableTo(dispose) 
+2
source share
2 answers

Perhaps you just need to update the version of the RxSwift library. It looks about the same as UI {Table | Collection} View rx bindings. Suppose you have a data source, for example:

 let items: Observable<[String]> = Observable.of(["Row1", "Row2", "Row3"]) 

To populate your UIPickerView:

 items.bind(to: yourPickerView.rx.itemTitles) { (row, element) in return element } .disposed(by: disposeBag) 

To process selected items:

 yourPickerView.rx.itemSelected .subscribe { (event) in switch event { case .next(let selected): print("You selected #\(selected.row)") default: break } } .disposed(by: disposeBag) 
+1
source

The provided data source of your collector is as follows:

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

you can implement the "binding" as follows:

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

Source: https://habr.com/ru/post/1264359/


All Articles