How to realize UIPickerView with RXSwift

拥有回忆 提交于 2019-12-12 11:46:45

问题


just like UITableView

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

回答1:


Maybe you just have to update your RxSwift library version. It looks like pretty much the same as UI{Table|Collection}View rx bindings. Supposing you have a data source like:

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 handle selected items:

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



回答2:


Swift4 example

Observable.just(["Yes", "No", "Maybe"])
                .bind(to: picker.rx.itemTitles) { _, item in
                    return "\(item)"
                }
                .disposed(by: disposeBag)



picker.rx.itemSelected
                .subscribe(onNext: { (row, value) in
                    NSLog("selected: \(row)")
                })
                .disposed(by: disposeBag)



回答3:


Provided data source of your picker looks like this:

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

you could implement 'binding' 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]
}).disposed(by: disposeBag)


来源:https://stackoverflow.com/questions/42108714/how-to-realize-uipickerview-with-rxswift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!