-
Notifications
You must be signed in to change notification settings - Fork 4
/
UTTableViewMultiOptionCell.m
218 lines (164 loc) · 4.37 KB
/
UTTableViewMultiOptionCell.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//
// UTTableViewMultiOptionCell.m
// RingFinder
//
// Created by Danny Morrow on 10/29/10.
// Copyright 2010 unitytheory.com. All rights reserved.
//
#import "UTTableViewMultiOptionCell.h"
@implementation UTTableViewMultiOptionCell
- (void) drawView
{
[super drawView];
self.textField.userInteractionEnabled = FALSE;
}
- (void) select
{
if (_isOpen)
{
[self hidePicker];
}
else
{
[self showPicker];
}
}
- (void) setSelectedIndex:(NSInteger)idx
{
if (_selectedIndex != idx)
{
_selectedIndex = idx;
self.value = [[self.data objectForKey:@"Values"] objectAtIndex:idx];
}
}
- (void) setValue:(id)val
{
if (_value != val)
{
BOOL found = FALSE;
for (id v in [self.data objectForKey:@"Values"])
{
if (v == val)
{
found = TRUE;
self.selectedIndex = [[self.data objectForKey:@"Values"] indexOfObject:val];
}
}
if (!found) self.selectedIndex = 0;
[super setValue:[[self.data objectForKey:@"Values"] objectAtIndex:self.selectedIndex]];
}
}
- (void) updateText
{
self.textField.text = [[self.data objectForKey:@"Titles"] objectAtIndex:self.selectedIndex];
}
#pragma mark -
#pragma mark UIPickerView
//creates the actual picker
- (void)pickerWithDefault:(NSInteger)defaultRow
{
if (!_pickerView)
{
_pickerView = [[UIView alloc] initWithFrame:CGRectMake(0, 480, 320, 261)];
[[[UIApplication sharedApplication] keyWindow] addSubview:_pickerView];
//create picker
UIPickerView* picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 45, 320, 161)];
picker.delegate=self;
picker.dataSource=self;
picker.showsSelectionIndicator=YES;
//if defaultstring is defined load the picker at that index.
[picker selectRow:defaultRow inComponent:0 animated:NO];
[_pickerView addSubview:picker];
UIBarButtonItem *done = [[UIBarButtonItem alloc]
initWithTitle:@"Done"
style:UIBarButtonItemStyleBordered
target:self action:@selector(hidePicker)];
UIBarButtonItem *flex = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:self action:nil];
UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];
[_pickerView addSubview:toolbar];
toolbar.barStyle=UIBarStyleBlackTranslucent;
toolbar.items = [NSArray arrayWithObjects:flex, done, nil];
}
}
- (void) showPicker
{
_isOpen = TRUE;
[self pickerWithDefault:self.selectedIndex];
[UIView beginAnimations:@"show" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:.3];
[UIView setAnimationBeginsFromCurrentState:TRUE];
CGRect f = _pickerView.frame;
f.origin.y = 480 - f.size.height;
_pickerView.frame = f;
[UIView commitAnimations];
if ([self.delegate respondsToSelector:@selector(cellDidBeginEditting:)])
{
[self.delegate cellDidBeginEditting:self];
}
}
- (void) hidePicker
{
_isOpen = FALSE;
DLog(@"hide picker");
[UIView beginAnimations:@"hide" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:.3];
[UIView setAnimationDelegate:self];
[UIView setAnimationBeginsFromCurrentState:TRUE];
[UIView setAnimationDidStopSelector:@selector(done:)];
CGRect f = _pickerView.frame;
f.origin.y = 480;
_pickerView.frame = f;
[UIView commitAnimations];
if ([self.delegate respondsToSelector:@selector(cellDidEndEditting:)])
{
[self.delegate cellDidEndEditting:self];
}
}
- (void)done:(id)sender
{
DLog(@"done");
if (_pickerView)
{
[_pickerView removeFromSuperview];
_pickerView = nil;
}
}
-(UIResponder *) responder
{
return self;
}
- (BOOL) resignFirstResponder
{
[self hidePicker];
[super resignFirstResponder];
return TRUE;
}
#pragma mark -
#pragma mark UIPickerViewDelegate
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
self.selectedIndex = row;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [[self.data objectForKey:@"Titles"] objectAtIndex:row];
}
#pragma mark -
#pragma mark UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [[self.data objectForKey:@"Values"] count];
}
- (void) dealloc
{
self.data = nil;
}
@end