forked from ReactiveX/RxSwift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add convenience UIPickerView rx extensions example
- Loading branch information
Showing
4 changed files
with
227 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
...RxExample/Examples/UIPickerViewExample/CustomPickerViewAdapterExampleViewController.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
// | ||
// CustomPickerViewAdapterExampleViewController.swift | ||
// RxExample | ||
// | ||
// Created by Sergey Shulga on 12/07/2017. | ||
// Copyright © 2017 Krunoslav Zaher. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
#if !RX_NO_MODULE | ||
import RxSwift | ||
import RxCocoa | ||
#endif | ||
|
||
final class CustomPickerViewAdapterExampleViewController: ViewController { | ||
@IBOutlet weak var pickerView1: UIPickerView! | ||
@IBOutlet weak var pickerView2: UIPickerView! | ||
@IBOutlet weak var pickerView3: UIPickerView! | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
|
||
Observable.just([[1, 2, 3], [5, 8, 13], [21, 34]]) | ||
.bind(to: pickerView1.rx.items(adapter: CustomStringPickerViewAdapter())) | ||
.disposed(by: disposeBag) | ||
|
||
Observable.just([[1, 2, 3], [5, 8, 13], [21, 34]]) | ||
.bind(to: pickerView2.rx.items(adapter: CustomAttributedStringPickerViewAdapter())) | ||
.disposed(by: disposeBag) | ||
|
||
Observable.just([[1, 2, 3], [5, 8, 13], [21, 34]]) | ||
.bind(to: pickerView3.rx.items(adapter: PickerViewViewAdapter())) | ||
.disposed(by: disposeBag) | ||
} | ||
} | ||
|
||
final class CustomStringPickerViewAdapter | ||
: NSObject | ||
, UIPickerViewDataSource | ||
, UIPickerViewDelegate | ||
, RxPickerViewDataSourceType { | ||
typealias Element = [[CustomStringConvertible]] | ||
private var items: [[CustomStringConvertible]] = [] | ||
|
||
func numberOfComponents(in pickerView: UIPickerView) -> Int { | ||
return items.count | ||
} | ||
|
||
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { | ||
return items[component].count | ||
} | ||
|
||
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { | ||
return items[component][row].description | ||
} | ||
|
||
func pickerView(_ pickerView: UIPickerView, observedEvent: Event<Element>) { | ||
UIBindingObserver(UIElement: self) { (adapter, items) in | ||
adapter.items = items | ||
pickerView.reloadAllComponents() | ||
}.on(observedEvent) | ||
} | ||
} | ||
|
||
final class CustomAttributedStringPickerViewAdapter | ||
: NSObject | ||
, UIPickerViewDataSource | ||
, UIPickerViewDelegate | ||
, RxPickerViewDataSourceType { | ||
typealias Element = [[CustomStringConvertible]] | ||
private var items: [[CustomStringConvertible]] = [] | ||
|
||
func numberOfComponents(in pickerView: UIPickerView) -> Int { | ||
return items.count | ||
} | ||
|
||
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { | ||
return items[component].count | ||
} | ||
|
||
func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? { | ||
return NSAttributedString(string: items[component][row].description, | ||
attributes: [ | ||
NSForegroundColorAttributeName: UIColor.cyan, | ||
NSUnderlineStyleAttributeName: NSUnderlineStyle.styleDouble.rawValue | ||
]) | ||
} | ||
|
||
func pickerView(_ pickerView: UIPickerView, observedEvent: Event<Element>) { | ||
UIBindingObserver(UIElement: self) { (adapter, items) in | ||
adapter.items = items | ||
pickerView.reloadAllComponents() | ||
}.on(observedEvent) | ||
} | ||
} | ||
|
||
final class PickerViewViewAdapter | ||
: NSObject | ||
, UIPickerViewDataSource | ||
, UIPickerViewDelegate | ||
, RxPickerViewDataSourceType { | ||
typealias Element = [[CustomStringConvertible]] | ||
private var items: [[CustomStringConvertible]] = [] | ||
|
||
func numberOfComponents(in pickerView: UIPickerView) -> Int { | ||
return items.count | ||
} | ||
|
||
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { | ||
return items[component].count | ||
} | ||
|
||
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView { | ||
let label = UILabel() | ||
label.text = items[component][row].description | ||
label.textColor = UIColor.orange | ||
label.font = UIFont.preferredFont(forTextStyle: .headline) | ||
label.textAlignment = .center | ||
return label | ||
} | ||
|
||
func pickerView(_ pickerView: UIPickerView, observedEvent: Event<Element>) { | ||
UIBindingObserver(UIElement: self) { (adapter, items) in | ||
adapter.items = items | ||
pickerView.reloadAllComponents() | ||
}.on(observedEvent) | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.