-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTableParentChildViewController.m
605 lines (507 loc) · 20.4 KB
/
TableParentChildViewController.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
//
// ExperimentViewController.m
// cambridge.teste2
//
// Created by Ricardo Freitas on 07/04/12.
// Copyright (c) 2012 Zimbora. All rights reserved.
//
#import "TableParentChildViewController.h"
//Enumerator that defines the type of a row
typedef enum _indexType {
ITparent = 1,
ITchild = 2,
ITaddParent = 3,
ITaddChild = 4
} IndexPathType;
static NSString* parentCellId = @"parentCellId";
static NSString* childCellId = @"childCellId";
static NSString* addParentCellId = @"addParentCellId";
static NSString* addChildCellId = @"addChildCellId";
@interface TableParentChildViewController ()
@end
@implementation TableParentChildViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.navigationItem.title = @"Abstract TableParentChild";
self.navigationItem.leftBarButtonItem = nil;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - concrete methods
#pragma mark Data Model
-(BOOL) canParentAddChild:(NSUInteger)parentIndex
{
return NO;
}
-(NSUInteger) numberOfParents
{
return 0;
}
-(NSUInteger) numberOfChildrenFromParent:(NSUInteger)parentIndex
{
return 0;
}
-(void)selectedParentAtIndex:(NSUInteger)parentIndex
{
[self.tableView deselectRowAtIndexPath:[NSIndexPath indexPathForParent:parentIndex] animated:YES];
}
-(void)selectedChildAtIndex:(NSUInteger)childIndex fromParent:(NSUInteger)parentIndex
{
[self.tableView deselectRowAtIndexPath:[NSIndexPath indexPathForChild:childIndex ofParent:parentIndex] animated:YES];
}
-(void)configureCell:(UITableViewCell*)cell ofParentAtIndex:(NSUInteger)parentIndex
{
cell.textLabel.text = @"Unamed parent cell";
}
-(void)configureCell:(UITableViewCell*)cell ofChildAtIndex:(NSUInteger)childIndex fromParent:(NSUInteger)parentIndex
{
cell.textLabel.text = @"Unamed child cell";
}
#pragma mark Data Model Editing methods
-(BOOL) selectedAddParent
{
return NO;
}
-(BOOL) selectedAddChildFromParent:(NSUInteger)parentIndex
{
return NO;
}
-(BOOL) moveParent:(NSUInteger)parentIndex ToIndex:(NSUInteger)targetIndex
{
return NO;
}
-(BOOL) moveChild:(NSUInteger)childIndex ToIndex:(NSUInteger)targetIndex FromParent:(NSUInteger)parentIndex
{
return NO;
}
-(BOOL) removeParentAtIndex:(NSUInteger)parentIndex
{
return NO;
}
-(BOOL) removeChildAtIndex:(NSUInteger)childIndex FromParent:(NSUInteger)parentIndex
{
return NO;
}
#pragma mark other concrete methods
-(NSString*) titleForAddParentCell
{
return @"Add Parent";
}
-(NSString*) titleForAddChildCellFromParent:(NSUInteger)parentIndex
{
return @"Add Child";
}
-(NSString*) headerTitleForParentSection:(NSUInteger)section
{
return nil;
}
-(void) save
{
NSLog(@"Not saving anything");
}
-(UITableViewCell*) tableCellForParentAtIndex:(NSUInteger)parentIndex
{
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:parentCellId];
if (cell==nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:parentCellId];
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
-(UITableViewCell*) tableCellForChildAtIndex:(NSUInteger)childIndex fromParent:(NSUInteger)parentIndex
{
UITableViewCell* cell = [self.tableView dequeueReusableCellWithIdentifier:childCellId];
if (cell==nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:childCellId];
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
#pragma mark -
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSUInteger outN = [self numberOfParents];
if (self.editing) {
outN++;
}
//NSLog(@"Number of sections:%d",outN);
return outN;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == [self numberOfParents])
{//id editing and last section
NSAssert(self.editing,@"state should be editing");
//NSLog(@"Numbers of rows:%d in last section",1);
return 1;
}
NSUInteger outN = [self numberOfChildrenFromParent:section]+1;//children + parent
if (self.editing && [self canParentAddChild:section]) {
outN++;//row for add child
}
//NSLog(@"Numbers of rows:%d in section:%d editing %d",outN,section,self.editing);
return outN;
}
-(NSString*) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [self headerTitleForParentSection:section];
}
#pragma mark editing
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
if (editing == self.editing) {
return;
}
[self.tableView beginUpdates];
if (editing) {
//NSLog(@"starting editing");
int lastSection = [self.tableView numberOfSections];
for (int i=0; i<lastSection; i++) {
int lastRow = [self.tableView numberOfRowsInSection:i];
if (lastRow>0 && [self canParentAddChild:i]) {
NSIndexPath* path = [NSIndexPath indexPathForRow:lastRow inSection:i];
//NSLog(@"inserting editing add child at section %d row %d",path.section,path.row);
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:path ] withRowAnimation:UITableViewRowAnimationTop];
}
}
NSIndexPath* path = [NSIndexPath indexPathForRow:0 inSection:lastSection];
//NSLog(@"inserting add parent at section %d row %d",path.section,path.row);
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:lastSection] withRowAnimation:UITableViewRowAnimationTop];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:path ] withRowAnimation:UITableViewRowAnimationTop];
}
else {
//NSLog(@"ending editing");
int lastSection = [self.tableView numberOfSections]-1;
for (int i=0; i<lastSection; i++) {
int lastRow = [self.tableView numberOfRowsInSection:i]-1;
if (lastRow>0 && [self canParentAddChild:i]) {
NSIndexPath* path = [NSIndexPath indexPathForRow:lastRow inSection:i];
//NSLog(@"deleting add child at section %d row %d",path.section,path.row);
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:path ] withRowAnimation:UITableViewRowAnimationTop];
}
}
NSIndexPath* path = [NSIndexPath indexPathForRow:0 inSection:lastSection];
//NSLog(@"deleting add parent at section %d row %d",path.section,path.row);
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:path ] withRowAnimation:UITableViewRowAnimationTop];
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:lastSection] withRowAnimation:UITableViewRowAnimationTop];
}
[super setEditing:editing animated:animated];
[self.tableView endUpdates];
}
//Commiting editing
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView beginUpdates];
//NSLog(@"commit editing for section %d row %d",indexPath.section,indexPath.row);
IndexPathType pathType = [self whatTypeIsIndexPath:indexPath];
switch (pathType) {
case ITparent:
NSAssert(editingStyle == UITableViewCellEditingStyleDelete,@"wrong state");
if ([self removeParentAtIndex:indexPath.section]){
[tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:YES];
}
else {
NSLog(@"Failed to remove parent.");
self.editing = NO;
}
break;
case ITchild:
NSAssert(editingStyle == UITableViewCellEditingStyleDelete,@"wrong state");
if ([self removeChildAtIndex:(indexPath.child) FromParent:indexPath.section]) {
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
}
else {
NSLog(@"Failed to remove child.");
self.editing = NO;
}
break;
case ITaddParent:
NSAssert(editingStyle == UITableViewCellEditingStyleInsert,@"wrong state");
[self addParent];
break;
case ITaddChild:
NSAssert(editingStyle == UITableViewCellEditingStyleInsert,@"wrong state");
[self addChildToParent:indexPath.section];
break;
}
[self save];
//NSLog(@"updates about to end");
[tableView endUpdates];
if ([self isIndexPathTypeOfParentType:pathType]) {
//NSLog(@"reloading section titles");
[tableView reloadSectionIndexTitles];
}
}
-(void) addParent
{
NSUInteger parentCount = [self numberOfParents];
if ([self selectedAddParent]) {
if (parentCount+1 == [self numberOfParents]) {
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:[self numberOfParents]-1] withRowAnimation:YES];
}
else {
@throw [NSException exceptionWithName:NSInternalInconsistencyException
reason:[NSString stringWithFormat:@"AddParentSelected did not create a new parent. Selector:%@", NSStringFromSelector(_cmd)]
userInfo:nil];
}
}
}
-(void) addChildToParent:(NSUInteger)parentIndex
{
NSUInteger childCount = [self numberOfChildrenFromParent:parentIndex];
if ([self selectedAddChildFromParent:parentIndex])
{
if (childCount+1==[self numberOfChildrenFromParent:parentIndex]) {
[self.tableView insertRow:[NSIndexPath indexPathForChild:childCount ofParent:parentIndex] withRowAnimation:UITableViewRowAnimationAutomatic];
}
else {
@throw [NSException exceptionWithName:NSInternalInconsistencyException
reason:[NSString stringWithFormat:@"AddChildSelected did not create a new child in parent %d. Selector:%@",parentIndex, NSStringFromSelector(_cmd)]
userInfo:nil];
}
}
}
//Row editing style determination
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
//NSLog(@"what editing style for section %d row %d",indexPath.section,indexPath.row);
if ([self isIndexPathOfAddType:indexPath]) {
return UITableViewCellEditingStyleInsert;
}
return UITableViewCellEditingStyleDelete;
}
#pragma mark cell configuration
//configuring cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//NSLog(@"configuring cell section %d row %d",indexPath.section,indexPath.row);
UITableViewCell* cell;
switch ([self whatTypeIsIndexPath:indexPath])
{
case ITparent:
//NSLog(@"configuring parent");
cell = [self tableCellForParentAtIndex:indexPath.section];
[self configureCell:cell ofParentAtIndex:indexPath.section];
break;
case ITchild:
{
NSUInteger childIndex = indexPath.child;
cell = [self tableCellForChildAtIndex:childIndex fromParent:indexPath.section];
[self configureCell:cell ofChildAtIndex:childIndex fromParent:indexPath.section];
break;
}
case ITaddParent:
//NSLog(@"configuring add parent");
cell = [tableView dequeueReusableCellWithIdentifier:addParentCellId];
if (cell==nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:addParentCellId];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.textLabel.text = [self titleForAddParentCell];
}
return cell;
case ITaddChild:
//NSLog(@"configuring add child");
cell = [tableView dequeueReusableCellWithIdentifier:addChildCellId];
if (cell==nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:addChildCellId];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.textLabel.text = [self titleForAddChildCellFromParent:indexPath.parent];
}
return cell;
}
return cell;
}
#pragma mark Row movement
// Commiting row move
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
if ([fromIndexPath isEqual:toIndexPath]) {
//NSLog(@"Row did not move.");
return;
}
BOOL moveSuccess = NO;
switch ([self whatTypeIsIndexPath:fromIndexPath]) {
case ITparent:
{
//NSLog(@"move confirmation of parent");
if ([self moveParent:fromIndexPath.section ToIndex:toIndexPath.section])
{
[tableView beginUpdates];
NSMutableArray* targetRows = [tableView arrayOfRowPathsOfSection:toIndexPath.section];
[targetRows removeObjectAtIndex:0];//exclude the moved row
[tableView deleteRowsAtIndexPaths:targetRows withRowAnimation:UITableViewRowAnimationAutomatic];//delete the previous rows in the target section
NSMutableArray* sourceRows = [tableView arrayOfRowPathsOfSection:fromIndexPath.section];
[tableView deleteRowsAtIndexPaths:sourceRows withRowAnimation:UITableViewRowAnimationAutomatic];//deletes the rows in the source section
//prepare the previous rows of the target section for insertion into the source section
[targetRows addRowValue:(-1)];//normalize the array, so that the row starts at 0
[targetRows replaceIndexPathsSectionWith:fromIndexPath.section];//make the section the source section
[tableView insertRowsAtIndexPaths:targetRows withRowAnimation:UITableViewRowAnimationAutomatic];
//prepare
[sourceRows addRowValue:(1)];//move the rows index by one, in order to give space to the already moved parent
[sourceRows replaceIndexPathsSectionWith:toIndexPath.section];
[tableView insertRowsAtIndexPaths:sourceRows withRowAnimation:UITableViewRowAnimationAutomatic];
//NSLog(@"ending update");
[tableView endUpdates];
moveSuccess = YES;
}
}
break;
case ITchild:
if ([self moveChild:(fromIndexPath.row-1) ToIndex:(toIndexPath.row-1) FromParent:fromIndexPath.section]) {
moveSuccess = YES;
}
break;
default:
abort();
break;
}
if (moveSuccess) {
[tableView reloadData];
[self save];
}
else {
[tableView moveRowAtIndexPath:toIndexPath toIndexPath:fromIndexPath];
[tableView reloadData];
}
}
// Contional row movement authorization
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self isIndexPathOfAddType:indexPath]) {
return NO;
}
return YES;
}
//Row movement logic
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
NSIndexPath* outIndexPath=sourceIndexPath;
//NSLog(@"proposed move to section %d and row %d from section %d and row %d",proposedDestinationIndexPath.section,proposedDestinationIndexPath.row,sourceIndexPath.section,sourceIndexPath.row);
switch ([self whatTypeIsIndexPath:sourceIndexPath]) {
case ITparent:
if (proposedDestinationIndexPath.section != sourceIndexPath.section && proposedDestinationIndexPath.section != [self numberOfParents])
{//move parent always to the head, first row, of the proposed section
//NSLog(@"sending proposal");
outIndexPath = [NSIndexPath indexPathForRow:0 inSection:proposedDestinationIndexPath.section];
}
break;
case ITchild:
if (proposedDestinationIndexPath.section == sourceIndexPath.section)
{//for now children cannot switch parents (parents)
NSUInteger outRow = proposedDestinationIndexPath.row;
NSUInteger outSection = proposedDestinationIndexPath.section;
//NSLog(@"number of children %d from parent %d",[self numberOfChildrenFromParent:outSection], [self numberOfParents]);
if (outRow == 0)
{// children cannot go to the head of a section, that position is reserverd for his parent
outRow = 1;
}
else if (outRow == [self numberOfChildrenFromParent:outSection]+1) {
outRow--;
}
//NSLog(@"sending proposal row %d section %d",outRow,outSection);
outIndexPath = [NSIndexPath indexPathForRow:outRow inSection:outSection];
return outIndexPath;
}
break;
default:
abort();
break;
}
return outIndexPath;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
switch ([self whatTypeIsIndexPath:indexPath]) {
case ITaddChild:
[self addChildToParent:indexPath.parent];
break;
case ITaddParent:
[self addParent];
break;
case ITchild:
[self selectedChildAtIndex:indexPath.child fromParent:indexPath.parent];
break;
case ITparent:
[self selectedParentAtIndex:indexPath.parent];
break;
default:
abort();
break;
}
}
#pragma mark - handy methods
-(void) reloadRowOfChild:(NSUInteger)childIndex fromParent:(NSUInteger)parentIndex
{
NSIndexPath* childPath = [NSIndexPath indexPathForChild:childIndex ofParent:parentIndex];
[self.tableView reloadRow:childPath withRowAnimation:UITableViewRowAnimationAutomatic];
}
-(void) reloadRowOfParent:(NSUInteger)parentIndex
{
[self.tableView reloadRow:[NSIndexPath indexPathForParent:parentIndex] withRowAnimation:UITableViewRowAnimationAutomatic];
}
-(UITableViewCell*) cellOfChild:(NSUInteger)childIndex fromParent:(NSUInteger)parentIndex
{
return [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForChild:childIndex ofParent:parentIndex]];
}
-(UITableViewCell*) cellOfParent:(NSUInteger)parentIndex
{
return [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForParent:parentIndex]];
}
#pragma mark type row checking
//type row checking methods
-(BOOL) isIndexPathOfAddType:(NSIndexPath*)indexPath
{
return [self isIndexPathTypeOfAddType:[self whatTypeIsIndexPath:indexPath]];
}
-(BOOL) isIndexPathTypeOfAddType:(IndexPathType)indexType
{
if (indexType == ITaddChild || indexType == ITaddParent) {
return YES;
}
return NO;
}
-(BOOL) isIndexPathTypeOfParentType:(IndexPathType)indexType
{
if (indexType == ITparent || indexType == ITaddParent) {
return YES;
}
return NO;
}
-(IndexPathType) whatTypeIsIndexPath:(NSIndexPath*)indexPath
{
int row = indexPath.row;
int section = indexPath.section;
IndexPathType outType;
if (self.editing && section == [self numberOfParents] && row==0) {
outType= ITaddParent;
}
else if (self.editing && row == [self numberOfChildrenFromParent:section]+1 ) {
outType= ITaddChild;
}
else if (row==0 && section < [self numberOfParents] ) {
outType= ITparent;
}
else if (row>0) {
outType= ITchild;
}
else {
NSLog(@"invalid state");
abort();
}
return outType;
}
@end