forked from peterpaulis/StaticDataTableViewController
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StaticDataTableViewController.m
executable file
·461 lines (300 loc) · 12 KB
/
StaticDataTableViewController.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
//
// StaticTableViewController.m
// StaticTableViewController 2.0
//
// Created by Peter Paulis on 31.1.2013.
// Copyright (c) 2013 Peter Paulis. All rights reserved.
//
#import "StaticDataTableViewController.h"
#define kBatchOperationNone 0
#define kBatchOperationInsert 1
#define kBatchOperationDelete 2
#define kBatchOperationUpdate 3
////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark OriginalRow
#pragma mark -
////////////////////////////////////////////////////////////////////////
@interface OriginalRow : NSObject
@property (nonatomic, assign) BOOL hidden;
@property (nonatomic, assign) BOOL hiddenReal;
@property (nonatomic, assign) BOOL hiddenPlanned;
@property (nonatomic, assign) int batchOperation;
@property (nonatomic, weak) UITableViewCell * cell;
@property (nonatomic, strong) NSIndexPath * originalIndexPath;
- (void)update;
@end
@implementation OriginalRow
- (BOOL)hidden {
return (self.hiddenPlanned || self.hiddenPlanned);
}
- (void)setHidden:(BOOL)hidden {
if ((!self.hiddenReal) && (hidden)) {
self.batchOperation = kBatchOperationDelete;
} else if ((self.hiddenReal) && (!hidden)) {
self.batchOperation = kBatchOperationInsert;
}
self.hiddenPlanned = hidden;
}
- (void)update {
if (!self.hidden) {
if (self.batchOperation == kBatchOperationNone) {
self.batchOperation = kBatchOperationUpdate;
}
}
}
@end
////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark OriginalSection
#pragma mark -
////////////////////////////////////////////////////////////////////////
@interface OriginalSection : NSObject
@property (nonatomic, strong) NSString * label;
@property (nonatomic, strong) NSMutableArray * rows;
@end
@implementation OriginalSection
- (int)numberOfVissibleRows {
int count = 0;
for (OriginalRow * or in self.rows) {
if (!or.hidden) {
++count;
}
}
return count;
}
- (int)vissibleRowIndexWithTableViewCell:(UITableViewCell *)cell {
int i = 0;
for (OriginalRow * or in self.rows) {
if (or.cell == cell) {
return i;
}
if (!or.hidden) {
++i;
}
}
return -1;
}
@end
////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark OriginalTable
#pragma mark -
////////////////////////////////////////////////////////////////////////
@interface OriginalTable : NSObject
@property (nonatomic, strong) NSMutableArray * sections;
@property (nonatomic, weak) UITableView * tableView;
@property (nonatomic, strong) NSMutableArray * insertIndexPaths;
@property (nonatomic, strong) NSMutableArray * deleteIndexPaths;
@property (nonatomic, strong) NSMutableArray * updateIndexPaths;
@end
@implementation OriginalTable
- (id)initWithTableView:(UITableView *)tableView {
self = [super init];
if (self) {
int numberOfSections = [tableView numberOfSections];
self.sections = [[NSMutableArray alloc] initWithCapacity:numberOfSections];
int totalNumberOfRows = 0;
for (int i = 0; i < numberOfSections; ++i) {
OriginalSection * originalSection = [OriginalSection new];
int numberOfRows = [tableView numberOfRowsInSection:i];
totalNumberOfRows += numberOfRows;
originalSection.rows = [[NSMutableArray alloc] initWithCapacity:numberOfRows];
for (int ii = 0; ii < numberOfRows; ++ii) {
OriginalRow * tableViewRow = [OriginalRow new];
NSIndexPath * ip = [NSIndexPath indexPathForRow:ii inSection:i];
tableViewRow.cell = [tableView.dataSource tableView:tableView cellForRowAtIndexPath:ip];
NSAssert(tableViewRow.cell != nil, @"cannot be nil");
tableViewRow.originalIndexPath = [NSIndexPath indexPathForRow:ii inSection:i];
originalSection.rows[ii] = tableViewRow;
}
self.sections[i] = originalSection;
}
self.insertIndexPaths = [[NSMutableArray alloc] initWithCapacity:totalNumberOfRows];
self.deleteIndexPaths = [[NSMutableArray alloc] initWithCapacity:totalNumberOfRows];
self.updateIndexPaths = [[NSMutableArray alloc] initWithCapacity:totalNumberOfRows];
self.tableView = tableView;
}
return self;
}
- (OriginalRow *)originalRowWithIndexPath:(NSIndexPath *)indexPath {
OriginalSection * oSection = self.sections[indexPath.section];
OriginalRow * oRow = oSection.rows[indexPath.row];
return oRow;
}
- (OriginalRow *)vissibleOriginalRowWithIndexPath:(NSIndexPath *)indexPath {
OriginalSection * oSection = self.sections[indexPath.section];
int vissibleIndex = -1;
for (int i = 0; i < [oSection.rows count]; ++i) {
OriginalRow * oRow = [oSection.rows objectAtIndex:i];
if (!oRow.hidden) {
++vissibleIndex;
}
if (indexPath.row == vissibleIndex) {
return oRow;
}
}
return nil;
}
- (OriginalRow *)originalRowWithTableViewCell:(UITableViewCell *)cell {
for (int i = 0; i < [self.sections count]; ++i) {
OriginalSection * os = self.sections[i];
for (int ii = 0; ii < [os.rows count]; ++ii) {
if ([os.rows[ii] cell] == cell) {
return os.rows[ii];
}
}
}
return nil;
}
- (NSIndexPath *)indexPathForInsertingOriginalRow:(OriginalRow *)originalRow {
OriginalSection * oSection = self.sections[originalRow.originalIndexPath.section];
int vissibleIndex = -1;
for (int i = 0; i < originalRow.originalIndexPath.row; ++i) {
OriginalRow * oRow = [oSection.rows objectAtIndex:i];
if (!oRow.hidden) {
++vissibleIndex;
}
}
return [NSIndexPath indexPathForRow:vissibleIndex + 1 inSection:originalRow.originalIndexPath.section];
}
- (NSIndexPath *)indexPathForDeletingOriginalRow:(OriginalRow *)originalRow {
OriginalSection * oSection = self.sections[originalRow.originalIndexPath.section];
int vissibleIndex = -1;
for (int i = 0; i < originalRow.originalIndexPath.row; ++i) {
OriginalRow * oRow = [oSection.rows objectAtIndex:i];
if (!oRow.hiddenReal) {
++vissibleIndex;
}
}
return [NSIndexPath indexPathForRow:vissibleIndex + 1 inSection:originalRow.originalIndexPath.section];
}
- (void)prepareUpdates {
[self.insertIndexPaths removeAllObjects];
[self.deleteIndexPaths removeAllObjects];
[self.updateIndexPaths removeAllObjects];
for (OriginalSection * os in self.sections) {
for (OriginalRow * or in os.rows) {
if (or.batchOperation == kBatchOperationDelete) {
NSIndexPath * ip = [self indexPathForDeletingOriginalRow:or];
[self.deleteIndexPaths addObject:ip];
} else if (or.batchOperation == kBatchOperationInsert) {
NSIndexPath * ip = [self indexPathForInsertingOriginalRow:or];
[self.insertIndexPaths addObject:ip];
} else if (or.batchOperation == kBatchOperationUpdate) {
NSIndexPath * ip = [self indexPathForInsertingOriginalRow:or];
[self.updateIndexPaths addObject:ip];
}
}
}
for (OriginalSection * os in self.sections) {
for (OriginalRow * or in os.rows) {
or.hiddenReal = or.hiddenPlanned;
or.batchOperation = kBatchOperationNone;
}
}
}
@end
////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark StaticDataTableViewController
#pragma mark -
////////////////////////////////////////////////////////////////////////
@interface StaticDataTableViewController ()
@property (nonatomic, strong) OriginalTable * originalTable;
@end
@implementation StaticDataTableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
#pragma mark - Lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
self.insertTableViewRowAnimation = UITableViewRowAnimationRight;
self.deleteTableViewRowAnimation = UITableViewRowAnimationLeft;
self.reloadTableViewRowAnimation = UITableViewRowAnimationMiddle;
self.originalTable = [[OriginalTable alloc] initWithTableView:self.tableView];
}
#pragma mark - Public
- (void)updateCell:(UITableViewCell *)cell {
OriginalRow * row = [self.originalTable originalRowWithTableViewCell:cell];
[row update];
}
- (void)updateCells:(NSArray *)cells {
for (UITableViewCell * cell in cells) {
[self updateCell:cell];
}
}
- (void)cell:(UITableViewCell *)cell setHidden:(BOOL)hidden {
OriginalRow * row = [self.originalTable originalRowWithTableViewCell:cell];
[row setHidden:hidden];
}
- (void)cells:(NSArray *)cells setHidden:(BOOL)hidden {
for (UITableViewCell * cell in cells) {
[self cell:cell setHidden:hidden];
}
}
- (BOOL)cellIsHidden:(UITableViewCell *)cell {
return [[self.originalTable originalRowWithTableViewCell:cell] hidden];
}
- (void)reloadDataAnimated:(BOOL)animated {
[self.originalTable prepareUpdates];
if (!animated) {
[self.tableView reloadData];
} else {
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:self.originalTable.updateIndexPaths withRowAnimation:self.reloadTableViewRowAnimation];
[self.tableView insertRowsAtIndexPaths:self.originalTable.insertIndexPaths withRowAnimation:self.insertTableViewRowAnimation];
[self.tableView deleteRowsAtIndexPaths:self.originalTable.deleteIndexPaths withRowAnimation:self.deleteTableViewRowAnimation];
[self.tableView endUpdates];
[self.tableView reloadData];
}
}
#pragma mark - TableView Data Source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.originalTable == nil) {
return [super tableView:tableView numberOfRowsInSection:section];
}
return [self.originalTable.sections[section] numberOfVissibleRows];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.originalTable == nil) {
return [super tableView:tableView cellForRowAtIndexPath:indexPath];
}
OriginalRow * or = [self.originalTable vissibleOriginalRowWithIndexPath:indexPath];
NSAssert(or.cell != nil, @"CANNOT BE NULL");
return or.cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.originalTable != nil) {
OriginalRow * or = [self.originalTable vissibleOriginalRowWithIndexPath:indexPath];
indexPath = or.originalIndexPath;
}
return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
CGFloat height = [super tableView:tableView heightForHeaderInSection:section];
if (self.originalTable == nil) {
return height;
}
if (!self.hideSectionsWithHiddenRows) {
return height;
}
OriginalSection * os = self.originalTable.sections[section];
if ([os numberOfVissibleRows] == 0) {
return 0;
} else {
return height;
}
return 0;
}
@end