Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxSwift and UIPickerView

Is there a way to bind a UIPickerView with an Observable?

For example for a UITableView I would do:

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

Is there something similar for UIPickerView ?

like image 373
TheoK Avatar asked Feb 17 '17 18:02

TheoK


1 Answers

As a matter of fact there is, in the RxCocoa library:

Example:

let items = Observable.just([
        "First Item",
        "Second Item",
        "Third Item"
    ])

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

There's also:

items
   .bind(to: pickerView.rx.items) { (row, element, view) in
       guard let myView = view as? MyView else {
           let view = MyView()
           view.configure(with: element)
           return view
       }
       myView.configure(with: element)
       return myView
   }
   .disposed(by: disposeBag)
like image 119
Daniel T. Avatar answered Sep 18 '22 14:09

Daniel T.