Skip to content

Commit

Permalink
Merge pull request #15 from kmcgill88/14-set-init-rows
Browse files Browse the repository at this point in the history
14 set init rows
  • Loading branch information
kmcgill88 committed Jul 22, 2017
2 parents 6219c96 + f8feb1b commit ec8c218
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 8 deletions.
4 changes: 4 additions & 0 deletions Example/McPicker/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ class ViewController: UIViewController {
mcPicker.toolbarButtonsColor = .white
mcPicker.toolbarBarTintColor = .darkGray
mcPicker.pickerBackgroundColor = .gray
mcPicker.pickerSelectRowsForComponents = [
0: [3: true],
1: [2: true]
]

if let barButton = sender as? UIBarButtonItem {
// Show as Popover
Expand Down
28 changes: 26 additions & 2 deletions Example/Tests/McPickerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ class McPickerTests: XCTestCase {
XCTAssertEqual(UIColor.purple, mcPicker.picker.backgroundColor)
}

func testDimissViews_callsAnimateViews() {
func testDimissViews_callsAnimateViewsWhenShow() {
// Given
//
class TestMcPicker: McPicker {
Expand Down Expand Up @@ -196,7 +196,7 @@ class McPickerTests: XCTestCase {
XCTAssertEqual(McPicker.AnimationDirection.out, mcPicker.direction)
}

func testDimissViews_calls() {
func testDimissViews_doesNotCallAnimateViewsWhenShowAsPopover() {
// Given
//
class TestVC: UIViewController {
Expand Down Expand Up @@ -227,4 +227,28 @@ class McPickerTests: XCTestCase {
XCTAssertNil(mcPicker.mcPickerPopoverViewController)
XCTAssertFalse(mcPicker.calledAnimateViews)
}

func testPickerSelectRowsForComponents() {
// Given
//
let data: [[String]] = [
["Sir", "Mr", "Mrs", "Miss"],
["Kevin", "Lauren", "Kibby", "Stella"]
]
let mcPicker = McPicker(data: data)

// When
//
mcPicker.pickerSelectRowsForComponents = [
0: [3: true],
1: [2: true]
]

// Then
//
XCTAssertEqual(3, mcPicker.picker.selectedRow(inComponent: 0))
XCTAssertEqual(2, mcPicker.picker.selectedRow(inComponent: 1))
XCTAssertEqual("Miss", mcPicker.pickerSelection[0]!)
XCTAssertEqual("Kibby", mcPicker.pickerSelection[1]!)
}
}
2 changes: 1 addition & 1 deletion McPicker.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'McPicker'
s.version = '0.3.1'
s.version = '0.4.0'
s.summary = 'McPicker is a UIPickerView drop-in solution with animations that is rotation ready.'

s.description = <<-DESC
Expand Down
15 changes: 15 additions & 0 deletions McPicker/Classes/McPicker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@ open class McPicker: UIView {
picker.backgroundColor = pickerBackgroundColor
}
}
/**
Sets the picker's components row position and picker selections to those String values.
[Int:[Int:Bool]] equates to [Component: [Row: isAnimated]
*/
public var pickerSelectRowsForComponents: [Int: [Int: Bool]]? {
didSet {
for component in pickerSelectRowsForComponents!.keys {
if let row = pickerSelectRowsForComponents![component]?.keys.first, let isAnimated = pickerSelectRowsForComponents![component]?.values.first {
pickerSelection[component] = pickerData[component][row]
picker.selectRow(row, inComponent: component, animated: isAnimated)
}
}
}
}

internal var popOverContentSize: CGSize {
return CGSize(width: Constant.pickerHeight + Constant.toolBarHeight, height: Constant.pickerHeight + Constant.toolBarHeight)
Expand Down
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,26 @@ McPicker.showAsPopover(data: data, fromViewController: self, barButtonItem: send

#### Customization
```swift
let data: [[String]] = [
["Sir", "Mr", "Mrs", "Miss"],
["Kevin", "Lauren", "Kibby", "Stella"]
]

let customLabel = UILabel()
customLabel.textAlignment = .center
customLabel.textColor = .white
customLabel.font = UIFont(name:"American Typewriter", size: 30)!

let data: [[String]] = [
["Sir", "Mr", "Mrs", "Miss"],
["Kevin", "Lauren", "Kibby", "Stella"]
]

let mcPicker = McPicker(data: data)
mcPicker.label = customLabel // Set your custom label
mcPicker.toolbarItemsFont = UIFont(name:"American Typewriter", size: 17)!
mcPicker.toolbarButtonsColor = .white
mcPicker.toolbarBarTintColor = .darkGray
mcPicker.pickerBackgroundColor = .gray
mcPicker.pickerSelectRowsForComponents = [
0: [3: true],
1: [2: true] // [Component: [Row: isAnimated]
]

if let barButton = sender as? UIBarButtonItem {
// Show as Popover
Expand All @@ -71,6 +75,9 @@ if let barButton = sender as? UIBarButtonItem {
}
```

##### The `selections`
McPicker's `doneHandler` passes back `selections: [Int : String]` as an argument. This is as simple as `[<Component Index>: <String of Selection>]` from the `data` you've passed in.

## Requirements
- iOS 8+
- Swift 3+
Expand Down

0 comments on commit ec8c218

Please sign in to comment.