Skip to content

Commit ec8c218

Browse files
authored
Merge pull request #15 from kmcgill88/14-set-init-rows
14 set init rows
2 parents 6219c96 + f8feb1b commit ec8c218

5 files changed

Lines changed: 58 additions & 8 deletions

File tree

Example/McPicker/ViewController.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ class ViewController: UIViewController {
8282
mcPicker.toolbarButtonsColor = .white
8383
mcPicker.toolbarBarTintColor = .darkGray
8484
mcPicker.pickerBackgroundColor = .gray
85+
mcPicker.pickerSelectRowsForComponents = [
86+
0: [3: true],
87+
1: [2: true]
88+
]
8589

8690
if let barButton = sender as? UIBarButtonItem {
8791
// Show as Popover

Example/Tests/McPickerTests.swift

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ class McPickerTests: XCTestCase {
166166
XCTAssertEqual(UIColor.purple, mcPicker.picker.backgroundColor)
167167
}
168168

169-
func testDimissViews_callsAnimateViews() {
169+
func testDimissViews_callsAnimateViewsWhenShow() {
170170
// Given
171171
//
172172
class TestMcPicker: McPicker {
@@ -196,7 +196,7 @@ class McPickerTests: XCTestCase {
196196
XCTAssertEqual(McPicker.AnimationDirection.out, mcPicker.direction)
197197
}
198198

199-
func testDimissViews_calls() {
199+
func testDimissViews_doesNotCallAnimateViewsWhenShowAsPopover() {
200200
// Given
201201
//
202202
class TestVC: UIViewController {
@@ -227,4 +227,28 @@ class McPickerTests: XCTestCase {
227227
XCTAssertNil(mcPicker.mcPickerPopoverViewController)
228228
XCTAssertFalse(mcPicker.calledAnimateViews)
229229
}
230+
231+
func testPickerSelectRowsForComponents() {
232+
// Given
233+
//
234+
let data: [[String]] = [
235+
["Sir", "Mr", "Mrs", "Miss"],
236+
["Kevin", "Lauren", "Kibby", "Stella"]
237+
]
238+
let mcPicker = McPicker(data: data)
239+
240+
// When
241+
//
242+
mcPicker.pickerSelectRowsForComponents = [
243+
0: [3: true],
244+
1: [2: true]
245+
]
246+
247+
// Then
248+
//
249+
XCTAssertEqual(3, mcPicker.picker.selectedRow(inComponent: 0))
250+
XCTAssertEqual(2, mcPicker.picker.selectedRow(inComponent: 1))
251+
XCTAssertEqual("Miss", mcPicker.pickerSelection[0]!)
252+
XCTAssertEqual("Kibby", mcPicker.pickerSelection[1]!)
253+
}
230254
}

McPicker.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'McPicker'
3-
s.version = '0.3.1'
3+
s.version = '0.4.0'
44
s.summary = 'McPicker is a UIPickerView drop-in solution with animations that is rotation ready.'
55

66
s.description = <<-DESC

McPicker/Classes/McPicker.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,21 @@ open class McPicker: UIView {
6060
picker.backgroundColor = pickerBackgroundColor
6161
}
6262
}
63+
/**
64+
Sets the picker's components row position and picker selections to those String values.
65+
66+
[Int:[Int:Bool]] equates to [Component: [Row: isAnimated]
67+
*/
68+
public var pickerSelectRowsForComponents: [Int: [Int: Bool]]? {
69+
didSet {
70+
for component in pickerSelectRowsForComponents!.keys {
71+
if let row = pickerSelectRowsForComponents![component]?.keys.first, let isAnimated = pickerSelectRowsForComponents![component]?.values.first {
72+
pickerSelection[component] = pickerData[component][row]
73+
picker.selectRow(row, inComponent: component, animated: isAnimated)
74+
}
75+
}
76+
}
77+
}
6378

6479
internal var popOverContentSize: CGSize {
6580
return CGSize(width: Constant.pickerHeight + Constant.toolBarHeight, height: Constant.pickerHeight + Constant.toolBarHeight)

README.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,26 @@ McPicker.showAsPopover(data: data, fromViewController: self, barButtonItem: send
3535

3636
#### Customization
3737
```swift
38+
let data: [[String]] = [
39+
["Sir", "Mr", "Mrs", "Miss"],
40+
["Kevin", "Lauren", "Kibby", "Stella"]
41+
]
42+
3843
let customLabel = UILabel()
3944
customLabel.textAlignment = .center
4045
customLabel.textColor = .white
4146
customLabel.font = UIFont(name:"American Typewriter", size: 30)!
4247

43-
let data: [[String]] = [
44-
["Sir", "Mr", "Mrs", "Miss"],
45-
["Kevin", "Lauren", "Kibby", "Stella"]
46-
]
47-
4848
let mcPicker = McPicker(data: data)
4949
mcPicker.label = customLabel // Set your custom label
5050
mcPicker.toolbarItemsFont = UIFont(name:"American Typewriter", size: 17)!
5151
mcPicker.toolbarButtonsColor = .white
5252
mcPicker.toolbarBarTintColor = .darkGray
5353
mcPicker.pickerBackgroundColor = .gray
54+
mcPicker.pickerSelectRowsForComponents = [
55+
0: [3: true],
56+
1: [2: true] // [Component: [Row: isAnimated]
57+
]
5458

5559
if let barButton = sender as? UIBarButtonItem {
5660
// Show as Popover
@@ -71,6 +75,9 @@ if let barButton = sender as? UIBarButtonItem {
7175
}
7276
```
7377

78+
##### The `selections`
79+
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.
80+
7481
## Requirements
7582
- iOS 8+
7683
- Swift 3+

0 commit comments

Comments
 (0)