diff --git a/RxExample/RxExample.xcodeproj/project.pbxproj b/RxExample/RxExample.xcodeproj/project.pbxproj index 2cd9b2870..ebed7be28 100644 --- a/RxExample/RxExample.xcodeproj/project.pbxproj +++ b/RxExample/RxExample.xcodeproj/project.pbxproj @@ -17,6 +17,7 @@ 2864D5F21D995FCD004F8484 /* Application+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2864D5F11D995FCD004F8484 /* Application+Extensions.swift */; }; 2864D5F31D995FCD004F8484 /* Application+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2864D5F11D995FCD004F8484 /* Application+Extensions.swift */; }; 8479BC721C3BDAD400FB8B54 /* ImagePickerController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8479BC701C3BCB9800FB8B54 /* ImagePickerController.swift */; }; + A5CD038F1F1670E50005A376 /* CustomPickerViewAdapterExampleViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5CD038E1F1670E50005A376 /* CustomPickerViewAdapterExampleViewController.swift */; }; AE51C1C91DE735D8005BAF5F /* APIWrappers.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = AE51C1C81DE735D8005BAF5F /* APIWrappers.storyboard */; }; AE51C1CB1DE735E3005BAF5F /* Calculator.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = AE51C1CA1DE735E3005BAF5F /* Calculator.storyboard */; }; AE51C1CD1DE735EF005BAF5F /* Geolocation.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = AE51C1CC1DE735EF005BAF5F /* Geolocation.storyboard */; }; @@ -432,6 +433,7 @@ 252C9F791F14115B00F5F951 /* SimpleUIPickerViewExample.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = SimpleUIPickerViewExample.storyboard; sourceTree = ""; }; 2864D5F11D995FCD004F8484 /* Application+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Application+Extensions.swift"; sourceTree = ""; }; 8479BC701C3BCB9800FB8B54 /* ImagePickerController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImagePickerController.swift; sourceTree = ""; }; + A5CD038E1F1670E50005A376 /* CustomPickerViewAdapterExampleViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CustomPickerViewAdapterExampleViewController.swift; sourceTree = ""; }; AE51C1C81DE735D8005BAF5F /* APIWrappers.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = APIWrappers.storyboard; sourceTree = ""; }; AE51C1CA1DE735E3005BAF5F /* Calculator.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = Calculator.storyboard; sourceTree = ""; }; AE51C1CC1DE735EF005BAF5F /* Geolocation.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = Geolocation.storyboard; sourceTree = ""; }; @@ -645,6 +647,7 @@ children = ( 252C9F771F14111800F5F951 /* SimplePickerViewExampleViewController.swift */, 252C9F791F14115B00F5F951 /* SimpleUIPickerViewExample.storyboard */, + A5CD038E1F1670E50005A376 /* CustomPickerViewAdapterExampleViewController.swift */, ); path = UIPickerViewExample; sourceTree = ""; @@ -1492,6 +1495,7 @@ C864BAD71C3332F10083833C /* DetailViewController.swift in Sources */, C822B1DF1C14CEAA0088A01A /* BindingExtensions.swift in Sources */, C864BAD91C3332F10083833C /* RandomUserAPI.swift in Sources */, + A5CD038F1F1670E50005A376 /* CustomPickerViewAdapterExampleViewController.swift in Sources */, C886A68B1D85AC9400653EE4 /* UIImagePickerController+Rx.swift in Sources */, C8477FF01D29DE8C0074454A /* AnimatableSectionModelType+ItemPath.swift in Sources */, C864BADF1C3332F10083833C /* UIImageView+Extensions.swift in Sources */, diff --git a/RxExample/RxExample/Examples/UIPickerViewExample/CustomPickerViewAdapterExampleViewController.swift b/RxExample/RxExample/Examples/UIPickerViewExample/CustomPickerViewAdapterExampleViewController.swift new file mode 100644 index 000000000..e700a129d --- /dev/null +++ b/RxExample/RxExample/Examples/UIPickerViewExample/CustomPickerViewAdapterExampleViewController.swift @@ -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) { + 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) { + 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) { + UIBindingObserver(UIElement: self) { (adapter, items) in + adapter.items = items + pickerView.reloadAllComponents() + }.on(observedEvent) + } +} + + diff --git a/RxExample/RxExample/Examples/UIPickerViewExample/SimplePickerViewExampleViewController.swift b/RxExample/RxExample/Examples/UIPickerViewExample/SimplePickerViewExampleViewController.swift index ce9f81369..7fdc8b61a 100644 --- a/RxExample/RxExample/Examples/UIPickerViewExample/SimplePickerViewExampleViewController.swift +++ b/RxExample/RxExample/Examples/UIPickerViewExample/SimplePickerViewExampleViewController.swift @@ -21,111 +21,28 @@ final class SimplePickerViewExampleViewController: ViewController { override func viewDidLoad() { super.viewDidLoad() - Observable.just([[1, 2, 3], [5, 8, 13], [21, 34]]) - .bind(to: pickerView1.rx.items(adapter: CustomStringPickerViewAdapter())) + Observable.just([1, 2, 3]) + .bind(to: pickerView1.rx.itemTitles) { _, item in + return "\(item)" + } .disposed(by: disposeBag) - Observable.just([[1, 2, 3], [5, 8, 13], [21, 34]]) - .bind(to: pickerView2.rx.items(adapter: CustomAttributedStringPickerViewAdapter())) + Observable.just([1, 2, 3]) + .bind(to: pickerView2.rx.itemAttributedTitles) { _, item in + return NSAttributedString(string: "\(item)", + attributes: [ + NSForegroundColorAttributeName: UIColor.cyan, + NSUnderlineStyleAttributeName: NSUnderlineStyle.styleDouble.rawValue + ]) + } .disposed(by: disposeBag) - Observable.just([[1, 2, 3], [5, 8, 13], [21, 34]]) - .bind(to: pickerView3.rx.items(adapter: PickerViewViewAdapter())) + Observable.just([UIColor.red, UIColor.green, UIColor.blue]) + .bind(to: pickerView3.rx.items) { _, item, _ in + let view = UIView() + view.backgroundColor = item + return view + } .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) { - 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) { - 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) { - UIBindingObserver(UIElement: self) { (adapter, items) in - adapter.items = items - pickerView.reloadAllComponents() - }.on(observedEvent) - } -} - - diff --git a/RxExample/RxExample/Examples/UIPickerViewExample/SimpleUIPickerViewExample.storyboard b/RxExample/RxExample/Examples/UIPickerViewExample/SimpleUIPickerViewExample.storyboard index 1ff03ed6f..2d6b661a7 100644 --- a/RxExample/RxExample/Examples/UIPickerViewExample/SimpleUIPickerViewExample.storyboard +++ b/RxExample/RxExample/Examples/UIPickerViewExample/SimpleUIPickerViewExample.storyboard @@ -1,20 +1,20 @@ - - + + - + - + - + @@ -27,7 +27,7 @@ - + @@ -49,6 +49,7 @@ + @@ -57,7 +58,74 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +