@@ -29,12 +29,20 @@ import ICInputAccessory
29
29
30
30
class ExampleViewController : UITableViewController {
31
31
32
- private let types : [ UIView . Type ] = [
32
+ private let showcases : [ UIView . Type ] = [
33
33
KeyboardDismissTextField . self,
34
34
TokenField . self,
35
- CustomizedTokenField . self
35
+ CustomizedTokenField . self,
36
+ OptionPickerControl< Language> . self
36
37
]
37
38
39
+ private lazy var languagePicker : OptionPickerControl < Language > = {
40
+ let picker = OptionPickerControl < Language > ( )
41
+ picker. options += Language . availableLanguages. map ( Option . init ( _: ) )
42
+ picker. addTarget ( self , action: . updateLanguage, for: . valueChanged)
43
+ return picker
44
+ } ( )
45
+
38
46
private lazy var flipButton : UIButton = {
39
47
let _button = UIButton ( type: . system)
40
48
_button. frame = CGRect ( x: 0 , y: 0 , width: UIScreen . main. bounds. width, height: 88 )
@@ -52,41 +60,45 @@ class ExampleViewController: UITableViewController {
52
60
53
61
// MARK: - UIViewController
54
62
55
- override func loadView ( ) {
56
- super. loadView ( )
63
+ override func viewDidLoad ( ) {
64
+ super. viewDidLoad ( )
57
65
tableView. rowHeight = 44
58
66
tableView. register ( ExampleCell . self, forCellReuseIdentifier: String ( describing: ExampleCell . self) )
59
67
tableView. tableFooterView = flipButton
60
68
tableView. tableFooterView? . isUserInteractionEnabled = true
69
+ view. addSubview ( languagePicker)
61
70
}
62
71
63
72
// MARK: - UITableViewDataSource
64
73
65
74
override func numberOfSections( in tableView: UITableView ) -> Int {
66
- return types . count
75
+ return showcases . count
67
76
}
68
77
69
78
override func tableView( _ tableView: UITableView , numberOfRowsInSection section: Int ) -> Int {
70
79
return 1
71
80
}
72
81
73
82
override func tableView( _ tableView: UITableView , titleForHeaderInSection section: Int ) -> String ? {
74
- switch types [ section] {
83
+ switch showcases [ section] {
75
84
case is KeyboardDismissTextField . Type :
76
85
return " Dismiss Keyboard "
77
86
case is TokenField . Type :
78
87
return " Text Field with Tokens "
79
88
case is CustomizedTokenField . Type :
80
89
return " Customize Token Field "
90
+ case is OptionPickerControl < Language > . Type :
91
+ return " Option Picker Control "
81
92
default :
82
93
return " "
83
94
}
84
95
}
85
96
86
97
override func tableView( _ tableView: UITableView , cellForRowAt indexPath: IndexPath ) -> UITableViewCell {
87
98
let cell = tableView. dequeueReusableCell ( withIdentifier: String ( describing: ExampleCell . self) , for: indexPath)
99
+ cell. accessoryType = . none
88
100
89
- switch types [ indexPath. section] {
101
+ switch showcases [ indexPath. section] {
90
102
case let type as KeyboardDismissTextField . Type :
91
103
let textField = type. init ( )
92
104
textField. leftViewMode = . always
@@ -107,6 +119,11 @@ class ExampleViewController: UITableViewController {
107
119
container. addSubview ( tokenField)
108
120
( cell as? ExampleCell ) ? . showcase = container
109
121
122
+ case is OptionPickerControl < Language > . Type :
123
+ ( cell as? ExampleCell ) ? . showcase = nil
124
+ cell. textLabel? . text = languagePicker. selectedOption. title
125
+ cell. accessoryType = . disclosureIndicator
126
+
110
127
default :
111
128
break
112
129
}
@@ -116,12 +133,25 @@ class ExampleViewController: UITableViewController {
116
133
// MARK: - UITableViewDelegate
117
134
118
135
override func tableView( _ tableView: UITableView , shouldHighlightRowAt indexPath: IndexPath ) -> Bool {
119
- return types [ indexPath. section] == CustomizedTokenField . self
136
+ switch showcases [ indexPath. section] {
137
+ case is CustomizedTokenField . Type :
138
+ return true
139
+ case is OptionPickerControl < Language > . Type :
140
+ return true
141
+ default :
142
+ return false
143
+ }
120
144
}
121
145
122
146
override func tableView( _ tableView: UITableView , didSelectRowAt indexPath: IndexPath ) {
123
- if types [ indexPath. section] == CustomizedTokenField . self {
147
+ switch showcases [ indexPath. section] {
148
+ case is CustomizedTokenField . Type :
124
149
present ( UINavigationController ( rootViewController: CustomizedTokenViewController ( ) ) , animated: true , completion: nil )
150
+ case is OptionPickerControl < Language > . Type :
151
+ tableView. deselectRow ( at: indexPath, animated: true )
152
+ languagePicker. becomeFirstResponder ( )
153
+ default :
154
+ break
125
155
}
126
156
}
127
157
@@ -134,6 +164,10 @@ class ExampleViewController: UITableViewController {
134
164
}
135
165
}
136
166
167
+ @objc fileprivate func updateLanguage( _ sender: UIControl ) {
168
+ tableView. reloadData ( )
169
+ }
170
+
137
171
}
138
172
139
173
@@ -142,4 +176,5 @@ class ExampleViewController: UITableViewController {
142
176
143
177
private extension Selector {
144
178
static let showStoryboard = #selector( ExampleViewController . showStoryboard ( _: ) )
179
+ static let updateLanguage = #selector( ExampleViewController . updateLanguage ( _: ) )
145
180
}
0 commit comments