-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUTTableViewPListDataSource.m
232 lines (190 loc) · 5.58 KB
/
UTTableViewPListDataSource.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
//
// UTTableViewPListDataSource.m
// RingFinder
//
// Created by Danny Morrow on 10/29/10.
// Copyright 2010 unitytheory.com. All rights reserved.
//
#import "UTTableViewPListDataSource.h"
#import "UTTableViewCell.h"
#import "UTTableViewMultiOptionCell.h"
#import "UTEditableTableView.h"
#import "UTTableViewPhotoPickerCell.h"
@implementation UTTableViewPListDataSource
@synthesize path = _path;
@synthesize plistData = _plistData;
@synthesize tableCellDelegate;
- (id) initWithPList:(NSString *)path
{
if (self = [super init])
{
self.path = path;
}
return self;
}
- (void) setPath:(NSString *) p
{
if (![_path isEqualToString:p])
{
_path = p;
[self parsePList];
}
}
- (void) parsePList
{
NSString* plistPath = [[NSBundle mainBundle] pathForResource:self.path ofType:nil inDirectory:@"plist"];
if (plistPath == nil)
{
plistPath = [[NSBundle mainBundle] pathForResource:self.path ofType:nil inDirectory:@""];
}
self.plistData = [NSMutableDictionary dictionaryWithContentsOfURL:[NSURL fileURLWithPath:plistPath]];
}
#pragma mark -
#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[self groups] count];
}
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
return [[self fieldsInGroup:section] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCellStyle style = UITableViewCellStyleValue1;
NSDictionary* dataModel = ((UTEditableTableView*) tableView).dataModel;
NSDictionary* data = [self dataAtIndexPath:indexPath];
if ([[data valueForKey:@"Type"] isEqualToString:kTextFieldSpecifier])
{
UTTableViewTextFieldCell* cell = (UTTableViewTextFieldCell *)[tableView dequeueReusableCellWithIdentifier:kTextFieldSpecifier];
if (!cell)
{
cell = [self textFieldCellFactory:style];
}
cell.delegate = self.tableCellDelegate;
cell.data = data;
cell.textField.keyboardType = [self keyboardFactory:[data valueForKey:@"KeyboardType"]];
cell.textField.autocapitalizationType = [self capitalizationFactory:[data valueForKey:@"CapitalizationType"]];
cell.value = [self setDefaultValue:dataModel data:data];
return cell;
}
else if ([[data valueForKey:@"Type"] isEqualToString:kMultiValueSpecifer])
{
UTTableViewMultiOptionCell* cell = (UTTableViewMultiOptionCell *)[tableView dequeueReusableCellWithIdentifier:kMultiValueSpecifer];
if (!cell)
{
cell = [self multioptionCellFactory:style];
}
cell.delegate = self.tableCellDelegate;
cell.data = data;
cell.value = [self setDefaultValue:dataModel data:data];
return cell;
}
else if ([[data valueForKey:@"Type"] isEqualToString:kPhotoPicker])
{
UTTableViewPhotoPickerCell* cell = (UTTableViewPhotoPickerCell *)[tableView dequeueReusableCellWithIdentifier:kPhotoPicker];
if (!cell)
{
cell = [self photoPickerCellFactory:style];
}
cell.delegate = self.tableCellDelegate;
cell.data = data;
cell.value = [self setDefaultValue:dataModel data:data];
return cell;
}
else
{
UITableViewCell *listCell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"default"];
if (!listCell)
{
listCell = [[UITableViewCell alloc] initWithStyle:style reuseIdentifier:@"default"];
}
return listCell;
}
}
- (id) setDefaultValue:(NSDictionary*) dataModel data:(NSDictionary *) data
{
if ([dataModel valueForKey:[data valueForKey:@"Key"]])
{
return [dataModel valueForKey:[data valueForKey:@"Key"]];
}
else
{
return [data valueForKey:@"DefaultValue"];
}
}
- (UIKeyboardType) keyboardFactory:(NSString*) keyboardType
{
if ([[keyboardType lowercaseString] isEqualToString: @"alphabet"])
{
return UIKeyboardTypeDefault;
}
else if ([[keyboardType lowercaseString] isEqualToString: @"email"])
{
return UIKeyboardTypeEmailAddress;
}
else if ([[keyboardType lowercaseString] isEqualToString: @"url"])
{
return UIKeyboardTypeURL;
}
else
{
return UIKeyboardTypeDefault;
}
}
- (UITextAutocapitalizationType) capitalizationFactory:(NSString*) capitalizationType
{
if ([[capitalizationType lowercaseString] isEqualToString: @"none"])
{
return UITextAutocapitalizationTypeNone;
}
else if ([[capitalizationType lowercaseString] isEqualToString: @"words"])
{
return UITextAutocapitalizationTypeWords;
}
else if ([[capitalizationType lowercaseString] isEqualToString: @"all"])
{
return UITextAutocapitalizationTypeAllCharacters;
}
else
{
return UITextAutocapitalizationTypeSentences;
}
}
#pragma mark -
#pragma mark field factories
- (UTTableViewTextFieldCell *) textFieldCellFactory:(UITableViewCellStyle) style
{
return [[UTTableViewTextFieldCell alloc] initWithStyle:style reuseIdentifier:kTextFieldSpecifier];
}
- (UTTableViewMultiOptionCell *) multioptionCellFactory:(UITableViewCellStyle) style
{
return [[UTTableViewMultiOptionCell alloc] initWithStyle:style reuseIdentifier:kMultiValueSpecifer];
}
- (UTTableViewPhotoPickerCell *) photoPickerCellFactory:(UITableViewCellStyle) style
{
return [[UTTableViewPhotoPickerCell alloc] initWithStyle:style reuseIdentifier:kPhotoPicker];
}
#pragma mark -
#pragma mark lookup
- (NSDictionary *) dataAtIndexPath:(NSIndexPath *)indexPath
{
return [[self fieldsInGroup:indexPath.section] objectAtIndex:indexPath.row];
}
- (NSDictionary *) groupAtIndex:(NSInteger)groupIndex
{
return [self.groups objectAtIndex:groupIndex];
}
- (NSArray *) groups
{
return [self.plistData objectForKey:@"Groups"];
}
- (NSArray *) fieldsInGroup:(NSInteger)groupIndex
{
return [[self groupAtIndex:groupIndex] objectForKey:@"Fields"];
}
- (void) dealloc
{
self.path = nil;
}
@end