-
Notifications
You must be signed in to change notification settings - Fork 0
/
CQFormView.m
316 lines (252 loc) · 8.52 KB
/
CQFormView.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
Copyright (C) 2012 Quentin Mathe
Author: Quentin Mathe <[email protected]>
Date: June 2012
License: MIT
*/
#import "CQFormView.h"
#import "CQFormItem.h"
#import "CQMacros.h"
@implementation CQFormView
{
NSArray *_sections;
NSArray *_sectionNames;
}
- (instancetype)initWithFrame: (CGRect)aRect style: (UITableViewStyle)aStyle
{
self = [super initWithFrame: aRect style: aStyle];
if (self == nil)
return nil;
_sections = [NSArray new];
_sectionNames = [NSArray new];
self.dataSource = self;
return self;
}
- (instancetype)initWithFrame: (CGRect)aRect
{
return [self initWithFrame: aRect style: UITableViewStyleGrouped];
}
- (instancetype)initWithCoder: (NSCoder *)aCoder
{
self = [super initWithCoder: aCoder];
if (self == nil)
return nil;
_sections = [NSArray new];
_sectionNames = [NSArray new];
[self setDataSource: self];
return self;
}
- (void)dealloc
{
//NSLog(@"Dealloc form view");
}
#pragma Generating Sections
- (NSArray *)sectionsFromItems: (NSArray *)allItems
sectionNames: (NSArray **)names
{
NSMutableArray *sections = [NSMutableArray new];
NSMutableArray *sectionNames = [NSMutableArray new];
for (CQFormItem *item in allItems)
{
if (![sectionNames containsObject: item.sectionName])
{
[sectionNames addObject: item.sectionName];
[sections addObject: [NSMutableArray new]];
assert(sectionNames.count == sections.count);
}
NSUInteger sectionIndex = [sectionNames indexOfObject: item.sectionName];
NSMutableArray *sectionItems = sections[sectionIndex];
[sectionItems addObject: item];
}
assert((sectionNames.count == 0 && sections.count == 1) || sectionNames.count == sections.count);
*names = [(NSArray *)sectionNames copy];
return [sections copy];
}
#pragma mark - Accessing Items
- (NSArray *)items
{
NSMutableArray *allItems = [NSMutableArray new];
for (NSArray *section in _sections)
{
[allItems addObjectsFromArray: section];
}
return [allItems copy];
}
- (void)setItems: (NSArray *)newItems
{
NSAssert(newItems.count == [NSSet setWithArray: newItems].count,
@"New items must contain no duplicates.");
NSSet *existingItems = [NSSet setWithArray: self.items];
NSArray *names = nil;
BOOL preparesForAnimatedUpdates = existingItems.count != 0 && newItems.count != 0;
NSIndexSet *removedSectionIndexes = nil;
NSIndexSet *insertedSectionIndexes = nil;
NSMutableArray *removedIndexPaths = [NSMutableArray array];
NSMutableArray *insertedIndexPaths = [NSMutableArray array];
if (preparesForAnimatedUpdates)
{
NSMutableSet *removedSectionNames = [NSMutableSet setWithArray: _sectionNames];
[removedSectionNames minusSet: [NSSet setWithArray: [newItems valueForKey: @"sectionName"]]];
removedSectionIndexes = [_sectionNames indexesOfObjectsPassingTest: ^ BOOL (NSString *sectionName, NSUInteger idx, BOOL *stop)
{
return [removedSectionNames containsObject: sectionName];
}];
NSMutableSet *removedItems = [NSMutableSet setWithSet: existingItems];
[removedItems minusSet: [NSSet setWithArray: newItems]];
[_sectionNames enumerateObjectsUsingBlock: ^(NSString *sectionName, NSUInteger sectionIndex, BOOL *stop)
{
NSArray *section = _sections[sectionIndex];
[section enumerateObjectsUsingBlock: ^(CQFormItem *item, NSUInteger row, BOOL *stop)
{
if ([removedItems containsObject: item])
{
[removedIndexPaths addObject: [NSIndexPath indexPathForRow: row
inSection: sectionIndex]];
}
}];
}];
}
_sections = [self sectionsFromItems: newItems sectionNames: &names];
_sectionNames = names;
if (preparesForAnimatedUpdates)
{
NSMutableSet *insertedSectionNames = [NSMutableSet setWithArray: [newItems valueForKey: @"sectionName"]];
[insertedSectionNames minusSet: [NSSet setWithArray: _sectionNames]];
insertedSectionIndexes = [_sectionNames indexesOfObjectsPassingTest: ^ BOOL (NSString *sectionName, NSUInteger idx, BOOL *stop)
{
return [insertedSectionNames containsObject: sectionName];
}];
NSMutableSet *insertedItems = [NSMutableSet setWithArray: newItems];
[insertedItems minusSet: existingItems];
[_sectionNames enumerateObjectsUsingBlock: ^(NSString *sectionName, NSUInteger sectionIndex, BOOL *stop)
{
NSArray *section = _sections[sectionIndex];
[section enumerateObjectsUsingBlock: ^(CQFormItem *item, NSUInteger row, BOOL *stop)
{
if ([insertedItems containsObject: item])
{
[insertedIndexPaths addObject: [NSIndexPath indexPathForRow: row
inSection: sectionIndex]];
}
}];
}];
}
if (preparesForAnimatedUpdates)
{
[self beginUpdates];
[self deleteRowsAtIndexPaths: removedIndexPaths
withRowAnimation: UITableViewRowAnimationAutomatic];
[self deleteSections: removedSectionIndexes
withRowAnimation: UITableViewRowAnimationAutomatic];
[self insertSections: insertedSectionIndexes
withRowAnimation: UITableViewRowAnimationAutomatic];
[self insertRowsAtIndexPaths: insertedIndexPaths
withRowAnimation: UITableViewRowAnimationAutomatic];
[self endUpdates];
}
else
{
[self reloadData];
}
}
- (CQFormItem *)itemForIndexPath: (NSIndexPath *)indexPath
{
return _sections[indexPath.section][indexPath.row];
}
- (NSIndexPath *)indexPathForItem: (CQFormItem *)item
{
INVALIDARG_EXCEPTION_TEST(item, [item.view isKindOfClass: [UITableViewCell class]]);
return [self indexPathForCell: (UITableViewCell *)item.view];
}
#pragma mark - Controlling Selection
// TODO: CQFormBuilder should call -setAllowsMultipleSelection:forSection: based
// on whether the edited property is multivalued or not.
- (BOOL)allowsMultipleSelectionForSection: (NSInteger)section
{
return NO;
}
- (NSArray *)optionItemsRelatedToItem: (CQFormItem *)item
{
NSIndexPath *indexPath = [self indexPathForCell: (UITableViewCell *)item.view];
return _sections[indexPath.section];
}
- (void)checkRowAtIndexPath: (NSIndexPath *)indexPath animated: (BOOL)animated
{
UITableViewCell *cell = [self cellForRowAtIndexPath: indexPath];
CQFormItem *item = [self itemForIndexPath: indexPath];
assert(item.view == cell);
cell.accessoryType = UITableViewCellAccessoryCheckmark;
if ([self allowsMultipleSelectionForSection: indexPath.section])
return;
NSArray *optionItems = [self optionItemsRelatedToItem: item];
[optionItems enumerateObjectsUsingBlock: ^(CQFormItem *otherItem, NSUInteger index, BOOL *stop)
{
if (otherItem == item)
return;
[self uncheckRowAtIndexPath: [NSIndexPath indexPathForRow: index
inSection: indexPath.section]];
}];
[self deselectRowAtIndexPath: indexPath animated: animated];
}
- (void)uncheckRowAtIndexPath: (NSIndexPath *)indexPath
{
CQFormItem *item = [self itemForIndexPath: indexPath];
UITableViewCell *cell = (UITableViewCell *)item.view;
if (cell.accessoryType != UITableViewCellAccessoryCheckmark)
return;
[item didDeselect];
cell.accessoryType = UITableViewCellAccessoryNone;
}
#pragma mark - Table View Data Source
- (UITableViewCell *)tableView: (UITableView *)tableView
cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
// FIXME: Support...
//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
CQFormItem *item = [self itemForIndexPath: indexPath];
assert(item != nil);
if ([item.view isKindOfClass: [UITableViewCell class]] == NO)
{
[NSException raise: NSInternalInconsistencyException
format: @"View %@ of %@ must be a valid UITableViewCell", item.view, item];
}
UITableViewCell *cell = (UITableViewCell *)item.view;
if (item.isChecked)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else if (item.contentViewController != nil)
{
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}
- (NSInteger)numberOfSectionsInTableView: (UITableView *)tableView
{
return _sections.count;
}
- (NSInteger)tableView: (UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
NSArray *sectionItems = _sections[section];
return sectionItems.count;
}
- (NSArray *)sectionIndexTitlesForTableView: (UITableView *)tableView
{
return nil;
}
- (NSString *)tableView: (UITableView *)tableView titleForHeaderInSection: (NSInteger)section
{
NSString *title = _sectionNames[section];
return ([title isEqual: @""] ? nil : title);
}
- (NSInteger) tableView: (UITableView *)tableView
sectionForSectionIndexTitle: (NSString *)title
atIndex: (NSInteger)index
{
return index;
}
- (void)refresh
{
[self.items makeObjectsPerformSelector: @selector(refreshViewFromRepresentedObject)];
}
@end