Skip to content

Commit

Permalink
Add UIPickerView adapter example
Browse files Browse the repository at this point in the history
  • Loading branch information
sergdort committed Jul 10, 2017
1 parent 4f3d803 commit eb6eb5a
Show file tree
Hide file tree
Showing 4 changed files with 269 additions and 22 deletions.
16 changes: 16 additions & 0 deletions RxExample/RxExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
075F13101B4E9D5A000D7861 /* APIWrappersViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 075F130F1B4E9D5A000D7861 /* APIWrappersViewController.swift */; };
07A5C3DB1B70B703001EFE5C /* CalculatorViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 07A5C3DA1B70B703001EFE5C /* CalculatorViewController.swift */; };
07E3C2331B03605B0010338D /* Dependencies.swift in Sources */ = {isa = PBXBuildFile; fileRef = 07E3C2321B03605B0010338D /* Dependencies.swift */; };
252C9F781F14111800F5F951 /* SimplePickerViewExampleViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 252C9F771F14111800F5F951 /* SimplePickerViewExampleViewController.swift */; };
252C9F7A1F14115B00F5F951 /* SimpleUIPickerViewExample.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 252C9F791F14115B00F5F951 /* SimpleUIPickerViewExample.storyboard */; };
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 */; };
Expand Down Expand Up @@ -426,6 +428,8 @@
075F130F1B4E9D5A000D7861 /* APIWrappersViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = APIWrappersViewController.swift; sourceTree = "<group>"; };
07A5C3DA1B70B703001EFE5C /* CalculatorViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CalculatorViewController.swift; sourceTree = "<group>"; };
07E3C2321B03605B0010338D /* Dependencies.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Dependencies.swift; path = Examples/Dependencies.swift; sourceTree = "<group>"; };
252C9F771F14111800F5F951 /* SimplePickerViewExampleViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SimplePickerViewExampleViewController.swift; sourceTree = "<group>"; };
252C9F791F14115B00F5F951 /* SimpleUIPickerViewExample.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = SimpleUIPickerViewExample.storyboard; sourceTree = "<group>"; };
2864D5F11D995FCD004F8484 /* Application+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Application+Extensions.swift"; sourceTree = "<group>"; };
8479BC701C3BCB9800FB8B54 /* ImagePickerController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImagePickerController.swift; sourceTree = "<group>"; };
AE51C1C81DE735D8005BAF5F /* APIWrappers.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = APIWrappers.storyboard; sourceTree = "<group>"; };
Expand Down Expand Up @@ -636,6 +640,15 @@
path = Calculator;
sourceTree = "<group>";
};
252C9F671F1410D600F5F951 /* UIPickerViewExample */ = {
isa = PBXGroup;
children = (
252C9F771F14111800F5F951 /* SimplePickerViewExampleViewController.swift */,
252C9F791F14115B00F5F951 /* SimpleUIPickerViewExample.storyboard */,
);
path = UIPickerViewExample;
sourceTree = "<group>";
};
8479BC6F1C3BCB4800FB8B54 /* ImagePicker */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -898,6 +911,7 @@
C86E2F2E1AE5A0CA00C31024 /* Examples */ = {
isa = PBXGroup;
children = (
252C9F671F1410D600F5F951 /* UIPickerViewExample */,
8479BC6F1C3BCB4800FB8B54 /* ImagePicker */,
C8BCD3E41C14A950005F1280 /* Numbers */,
C8BCD3E81C14B015005F1280 /* SimpleValidation */,
Expand Down Expand Up @@ -1390,6 +1404,7 @@
AE51C1D51DE73613005BAF5F /* ImagePicker.storyboard in Resources */,
AE51C1CD1DE735EF005BAF5F /* Geolocation.storyboard in Resources */,
AE51C1D31DE73608005BAF5F /* GitHubSignup2.storyboard in Resources */,
252C9F7A1F14115B00F5F951 /* SimpleUIPickerViewExample.storyboard in Resources */,
AE51C1DD1DE73649005BAF5F /* SimpleValidation.storyboard in Resources */,
C8DF92E31B0B32DA009BCF9A /* LaunchScreen.xib in Resources */,
C8C46DA91B47F7110020D71E /* WikipediaImageCell.xib in Resources */,
Expand Down Expand Up @@ -1504,6 +1519,7 @@
C8984CD11C36BC3E001E4272 /* NumberCell.swift in Sources */,
C8A2A2C81B4049E300F11F09 /* PseudoRandomGenerator.swift in Sources */,
C8D132151C42B54B00B59FFF /* UIImagePickerController+RxCreate.swift in Sources */,
252C9F781F14111800F5F951 /* SimplePickerViewExampleViewController.swift in Sources */,
C8477FF71D29DE8C0074454A /* Differentiator.swift in Sources */,
C8984CD51C36BC3E001E4272 /* PartialUpdatesViewController.swift in Sources */,
C88CB7261D8F253D0021D83F /* RxImagePickerDelegateProxy.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
//
// SimplePickerViewExampleViewController.swift
// RxExample
//
// Created by sergdort on 10/07/2017.
// Copyright © 2017 Krunoslav Zaher. All rights reserved.
//

import UIKit
#if !RX_NO_MODULE
import RxSwift
import RxCocoa
#endif

final class SimplePickerViewExampleViewController: 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)
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12118" systemVersion="16E195" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="U76-um-TqZ">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12086"/>
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
<capability name="Constraints with non-1.0 multipliers" minToolsVersion="5.1"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--Simple Picker View Example View Controller-->
<scene sceneID="iy0-gg-lTJ">
<objects>
<viewController id="U76-um-TqZ" customClass="SimplePickerViewExampleViewController" customModule="RxExample_iOS" customModuleProvider="target" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="61a-Hs-mqd"/>
<viewControllerLayoutGuide type="bottom" id="I6Z-oN-WS8"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="8lm-KZ-WhU">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<pickerView contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="FUv-qb-kPm">
<rect key="frame" x="0.0" y="0.0" width="375" height="222.5"/>
</pickerView>
<pickerView contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="7Qw-EC-A7z">
<rect key="frame" x="0.0" y="444" width="375" height="222.5"/>
</pickerView>
<pickerView contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Jek-p5-MeP">
<rect key="frame" x="0.0" y="222" width="375" height="222.5"/>
</pickerView>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstItem="Jek-p5-MeP" firstAttribute="leading" secondItem="8lm-KZ-WhU" secondAttribute="leadingMargin" constant="-16" id="0To-gV-vsk"/>
<constraint firstItem="Jek-p5-MeP" firstAttribute="top" secondItem="FUv-qb-kPm" secondAttribute="bottom" constant="-0.5" id="2Ob-x4-V3b"/>
<constraint firstItem="7Qw-EC-A7z" firstAttribute="height" secondItem="Jek-p5-MeP" secondAttribute="height" id="8ka-rT-fzR"/>
<constraint firstItem="I6Z-oN-WS8" firstAttribute="top" secondItem="7Qw-EC-A7z" secondAttribute="bottom" constant="0.5" id="9BL-qX-gl5"/>
<constraint firstItem="Jek-p5-MeP" firstAttribute="height" secondItem="FUv-qb-kPm" secondAttribute="height" id="S9j-bB-7dM"/>
<constraint firstAttribute="trailingMargin" secondItem="FUv-qb-kPm" secondAttribute="trailing" constant="-16" id="SWq-eM-fu4"/>
<constraint firstItem="7Qw-EC-A7z" firstAttribute="leading" secondItem="8lm-KZ-WhU" secondAttribute="leadingMargin" constant="-16" id="Suo-sl-F3b"/>
<constraint firstItem="FUv-qb-kPm" firstAttribute="top" secondItem="61a-Hs-mqd" secondAttribute="bottom" constant="-20" id="WcE-mF-beh"/>
<constraint firstItem="FUv-qb-kPm" firstAttribute="height" secondItem="8lm-KZ-WhU" secondAttribute="height" multiplier="1:3" id="YBK-o9-J8J"/>
<constraint firstAttribute="trailingMargin" secondItem="7Qw-EC-A7z" secondAttribute="trailing" constant="-16" id="ltJ-CE-JjD"/>
<constraint firstItem="FUv-qb-kPm" firstAttribute="leading" secondItem="8lm-KZ-WhU" secondAttribute="leadingMargin" constant="-16" id="ncD-bk-j9H"/>
<constraint firstAttribute="trailingMargin" secondItem="Jek-p5-MeP" secondAttribute="trailing" constant="-16" id="xxa-q4-NBW"/>
</constraints>
</view>
<connections>
<outlet property="pickerView1" destination="FUv-qb-kPm" id="tT9-eF-nRW"/>
<outlet property="pickerView2" destination="Jek-p5-MeP" id="PGs-Oy-25Y"/>
<outlet property="pickerView3" destination="7Qw-EC-A7z" id="hQX-Yf-u5o"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="eYS-P5-6qD" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="97.5" y="147.5"/>
</scene>
</scenes>
</document>
Loading

0 comments on commit eb6eb5a

Please sign in to comment.