6

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 ?

TheoK
  • 3,601
  • 5
  • 27
  • 37
  • There is nothing usable out of the box from RxSwift's repository. One could look at the code used for `UITableView` bindings to come up with something similar for `UIPickerView`, but you'd need a pretty good experience with RxSwift to get a grasp of how that works (at least, **I** tried and failed) – tomahh Feb 19 '17 at 09:32
  • 1
    Please take a look at this answer: https://stackoverflow.com/a/46225009/1953178 – Amr Hossam Feb 04 '18 at 08:26

2 Answers2

2

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)
Daniel T.
  • 32,821
  • 6
  • 50
  • 72
  • How to do this for `UIDatePicker` in `RxSwift` ? Any tutorial ? – McDonal_11 Nov 04 '19 at 04:43
  • The date picker class already has a reactive extension. Look at `rx.date`. – Daniel T. Nov 05 '19 at 02:33
  • https://github.com/ReactiveX/RxSwift/blob/master/RxCocoa/iOS/UIDatePicker%2BRx.swift , I have read this link. I don't know how to get chosen date from `UIDatePicker`. Can u guide me? – McDonal_11 Nov 05 '19 at 05:35
  • I have mailed u regarding this doubt. datePicker.rx.date .subscribe { (datee) in print("chosen date ", datee) }.disposed(by: disposeBag) . Is this right way to get chose date from `UIDatePickerView` ? – McDonal_11 Nov 05 '19 at 11:52
  • That's not appropriate. If you have a question about something. Ask it on this site or in the RxSwift slack channel. Someone will answer it (maybe even me.) – Daniel T. Nov 06 '19 at 11:23
  • Hi Daniel. In the first example, 'element.title' doesn't seem right. Element is just a string here, isn't it? – Dale Mar 09 '20 at 00:28
  • Good catch @Dale I fixed it. – Daniel T. Mar 09 '20 at 00:30
0

Provided data source of your picker looks like this:

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

you could implement 'binding' you need in this way:

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)
Valeriy Van
  • 1,851
  • 16
  • 19