This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
/
TTTableViewController.m
894 lines (731 loc) · 30.7 KB
/
TTTableViewController.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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
//
// Copyright 2009-2010 Facebook
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "Three20UI/TTTableViewController.h"
// UI
#import "Three20UI/TTNavigator.h"
#import "Three20UI/TTActivityLabel.h"
#import "Three20UI/TTErrorView.h"
#import "Three20UI/TTListDataSource.h"
#import "Three20UI/TTTableView.h"
#import "Three20UI/TTTableViewDelegate.h"
#import "Three20UI/TTTableViewVarHeightDelegate.h"
#import "Three20UI/UIViewAdditions.h"
#import "Three20UI/UITableViewAdditions.h"
// UINavigator
#import "Three20UINavigator/TTURLObject.h"
// UICommon
#import "Three20UICommon/TTGlobalUICommon.h"
#import "Three20UICommon/UIViewControllerAdditions.h"
// Style
#import "Three20Style/TTGlobalStyle.h"
#import "Three20Style/TTDefaultStyleSheet.h"
// Core
#import "Three20Core/TTCorePreprocessorMacros.h"
#import "Three20Core/TTGlobalCoreLocale.h"
#import "Three20Core/TTGlobalCoreRects.h"
#import "Three20Core/TTDebug.h"
#import "Three20Core/TTDebugFlags.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
@implementation TTTableViewController
@synthesize tableView = _tableView;
@synthesize tableBannerView = _tableBannerView;
@synthesize tableOverlayView = _tableOverlayView;
@synthesize loadingView = _loadingView;
@synthesize errorView = _errorView;
@synthesize emptyView = _emptyView;
@synthesize menuView = _menuView;
@synthesize tableViewStyle = _tableViewStyle;
@synthesize variableHeightRows = _variableHeightRows;
@synthesize showTableShadows = _showTableShadows;
@synthesize dataSource = _dataSource;
///////////////////////////////////////////////////////////////////////////////////////////////////
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
_lastInterfaceOrientation = self.interfaceOrientation;
_tableViewStyle = UITableViewStylePlain;
}
return self;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (id)initWithStyle:(UITableViewStyle)style {
if (self = [self initWithNibName:nil bundle:nil]) {
_tableViewStyle = style;
}
return self;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)dealloc {
_tableView.delegate = nil;
_tableView.dataSource = nil;
TT_RELEASE_SAFELY(_tableDelegate);
TT_RELEASE_SAFELY(_dataSource);
TT_RELEASE_SAFELY(_tableView);
TT_RELEASE_SAFELY(_loadingView);
TT_RELEASE_SAFELY(_errorView);
TT_RELEASE_SAFELY(_emptyView);
TT_RELEASE_SAFELY(_tableOverlayView);
TT_RELEASE_SAFELY(_tableBannerView);
[super dealloc];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Private
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)createInterstitialModel {
self.dataSource = [[[TTTableViewInterstitialDataSource alloc] init] autorelease];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (NSString*)defaultTitleForLoading {
return TTLocalizedString(@"Loading...", @"");
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)updateTableDelegate {
if (!_tableView.delegate) {
[_tableDelegate release];
_tableDelegate = [[self createDelegate] retain];
// You need to set it to nil before changing it or it won't have any effect
_tableView.delegate = nil;
_tableView.delegate = _tableDelegate;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)addToOverlayView:(UIView*)view {
if (!_tableOverlayView) {
CGRect frame = [self rectForOverlayView];
_tableOverlayView = [[UIView alloc] initWithFrame:frame];
_tableOverlayView.autoresizesSubviews = YES;
_tableOverlayView.autoresizingMask = UIViewAutoresizingFlexibleWidth
| UIViewAutoresizingFlexibleBottomMargin;
NSInteger tableIndex = [_tableView.superview.subviews indexOfObject:_tableView];
if (tableIndex != NSNotFound) {
[_tableView.superview addSubview:_tableOverlayView];
}
}
view.frame = _tableOverlayView.bounds;
view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[_tableOverlayView addSubview:view];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)resetOverlayView {
if (_tableOverlayView && !_tableOverlayView.subviews.count) {
[_tableOverlayView removeFromSuperview];
TT_RELEASE_SAFELY(_tableOverlayView);
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)addSubviewOverTableView:(UIView*)view {
NSInteger tableIndex = [_tableView.superview.subviews
indexOfObject:_tableView];
if (NSNotFound != tableIndex) {
[_tableView.superview addSubview:view];
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)layoutOverlayView {
if (_tableOverlayView) {
_tableOverlayView.frame = [self rectForOverlayView];
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)layoutBannerView {
if (_tableBannerView) {
_tableBannerView.frame = [self rectForBannerView];
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)fadeOutView:(UIView*)view {
[view retain];
[UIView beginAnimations:nil context:view];
[UIView setAnimationDuration:TT_TRANSITION_DURATION];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(fadingOutViewDidStop:finished:context:)];
view.alpha = 0;
[UIView commitAnimations];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)fadingOutViewDidStop:(NSString*)animationID finished:(NSNumber*)finished
context:(void*)context {
UIView* view = (UIView*)context;
[view removeFromSuperview];
[view release];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)hideMenuAnimationDidStop:(NSString*)animationID finished:(NSNumber*)finished
context:(void*)context {
UIView* menuView = (UIView*)context;
[menuView removeFromSuperview];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark UIViewController
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)loadView {
[super loadView];
self.tableView;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewDidUnload {
[super viewDidUnload];
_tableView.delegate = nil;
_tableView.dataSource = nil;
TT_RELEASE_SAFELY(_tableDelegate);
TT_RELEASE_SAFELY(_tableView);
[_tableBannerView removeFromSuperview];
TT_RELEASE_SAFELY(_tableBannerView);
[_tableOverlayView removeFromSuperview];
TT_RELEASE_SAFELY(_tableOverlayView);
[_loadingView removeFromSuperview];
TT_RELEASE_SAFELY(_loadingView);
[_errorView removeFromSuperview];
TT_RELEASE_SAFELY(_errorView);
[_emptyView removeFromSuperview];
TT_RELEASE_SAFELY(_emptyView);
[_menuView removeFromSuperview];
TT_RELEASE_SAFELY(_menuView);
[_menuCell removeFromSuperview];
TT_RELEASE_SAFELY(_menuCell);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (_lastInterfaceOrientation != self.interfaceOrientation) {
_lastInterfaceOrientation = self.interfaceOrientation;
[_tableView reloadData];
} else if ([_tableView isKindOfClass:[TTTableView class]]) {
TTTableView* tableView = (TTTableView*)_tableView;
tableView.highlightedLabel = nil;
tableView.showShadows = _showTableShadows;
}
[_tableView deselectRowAtIndexPath:[_tableView indexPathForSelectedRow] animated:YES];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self hideMenu:YES];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:animated];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark UTViewController (TTCategory)
///////////////////////////////////////////////////////////////////////////////////////////////////
- (BOOL)persistView:(NSMutableDictionary*)state {
CGFloat scrollY = _tableView.contentOffset.y;
[state setObject:[NSNumber numberWithFloat:scrollY] forKey:@"scrollOffsetY"];
return [super persistView:state];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)restoreView:(NSDictionary*)state {
CGFloat scrollY = [[state objectForKey:@"scrollOffsetY"] floatValue];
if (scrollY) {
CGFloat maxY = _tableView.contentSize.height - _tableView.height;
if (scrollY <= maxY) {
_tableView.contentOffset = CGPointMake(0, scrollY);
} else {
_tableView.contentOffset = CGPointMake(0, maxY);
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark TTViewController
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)keyboardDidAppear:(BOOL)animated withBounds:(CGRect)bounds {
[super keyboardDidAppear:animated withBounds:bounds];
self.tableView.frame = TTRectContract(self.tableView.frame, 0, bounds.size.height);
[self.tableView scrollFirstResponderIntoView];
[self layoutOverlayView];
[self layoutBannerView];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)keyboardWillDisappear:(BOOL)animated withBounds:(CGRect)bounds {
[super keyboardWillDisappear:animated withBounds:bounds];
// If we do this when there is currently no table view, we can get into a weird loop where the
// table view gets doubly-initialized. self.tableView will try to initialize it; this will call
// self.view, which will call -loadView, which often calls self.tableView, which initializes it.
if (_tableView) {
CGRect previousFrame = self.tableView.frame;
self.tableView.frame = TTRectContract(self.tableView.frame, 0, -bounds.size.height);
// There's any number of edge cases wherein a table view controller will get this callback but
// it shouldn't resize itself -- e.g. when a controller has the keyboard up, and then drills
// down into this controller. This is a sanity check to avoid situations where the table
// extends way off the bottom of the screen and becomes unusable.
if (self.tableView.height > self.view.bounds.size.height) {
self.tableView.frame = previousFrame;
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)keyboardDidDisappear:(BOOL)animated withBounds:(CGRect)bounds {
[super keyboardDidDisappear:animated withBounds:bounds];
[self layoutOverlayView];
[self layoutBannerView];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark TTModelViewController
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)beginUpdates {
[super beginUpdates];
[_tableView beginUpdates];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)endUpdates {
[super endUpdates];
[_tableView endUpdates];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (BOOL)canShowModel {
if ([_dataSource respondsToSelector:@selector(numberOfSectionsInTableView:)]) {
NSInteger numberOfSections = [_dataSource numberOfSectionsInTableView:_tableView];
if (!numberOfSections) {
return NO;
} else if (numberOfSections == 1) {
NSInteger numberOfRows = [_dataSource tableView:_tableView numberOfRowsInSection:0];
return numberOfRows > 0;
} else {
return YES;
}
} else {
NSInteger numberOfRows = [_dataSource tableView:_tableView numberOfRowsInSection:0];
return numberOfRows > 0;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)didLoadModel:(BOOL)firstTime {
[super didLoadModel:firstTime];
[_dataSource tableViewDidLoadModel:_tableView];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)didShowModel:(BOOL)firstTime {
[super didShowModel:firstTime];
if (firstTime) {
[_tableView flashScrollIndicators];
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)showModel:(BOOL)show {
[self hideMenu:YES];
if (show) {
[self updateTableDelegate];
_tableView.dataSource = _dataSource;
} else {
_tableView.dataSource = nil;
}
[_tableView reloadData];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)showLoading:(BOOL)show {
if (show) {
if (!self.model.isLoaded || ![self canShowModel]) {
NSString* title = _dataSource
? [_dataSource titleForLoading:NO]
: [self defaultTitleForLoading];
if (title.length) {
TTActivityLabel* label = [[[TTActivityLabel alloc] initWithStyle:TTActivityLabelStyleWhiteBox]
autorelease];
label.text = title;
label.backgroundColor = _tableView.backgroundColor;
self.loadingView = label;
}
}
} else {
self.loadingView = nil;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)showError:(BOOL)show {
if (show) {
if (!self.model.isLoaded || ![self canShowModel]) {
NSString* title = [_dataSource titleForError:_modelError];
NSString* subtitle = [_dataSource subtitleForError:_modelError];
UIImage* image = [_dataSource imageForError:_modelError];
if (title.length || subtitle.length || image) {
TTErrorView* errorView = [[[TTErrorView alloc] initWithTitle:title
subtitle:subtitle
image:image] autorelease];
errorView.backgroundColor = _tableView.backgroundColor;
self.errorView = errorView;
} else {
self.errorView = nil;
}
_tableView.dataSource = nil;
[_tableView reloadData];
}
} else {
self.errorView = nil;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)showEmpty:(BOOL)show {
if (show) {
NSString* title = [_dataSource titleForEmpty];
NSString* subtitle = [_dataSource subtitleForEmpty];
UIImage* image = [_dataSource imageForEmpty];
if (title.length || subtitle.length || image) {
TTErrorView* errorView = [[[TTErrorView alloc] initWithTitle:title
subtitle:subtitle
image:image] autorelease];
errorView.backgroundColor = _tableView.backgroundColor;
self.emptyView = errorView;
} else {
self.emptyView = nil;
}
_tableView.dataSource = nil;
[_tableView reloadData];
} else {
self.emptyView = nil;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark TTModelDelegate
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)model:(id<TTModel>)model didUpdateObject:(id)object atIndexPath:(NSIndexPath*)indexPath {
if (model == _model) {
if (_isViewAppearing && _flags.isShowingModel) {
if ([_dataSource respondsToSelector:@selector(tableView:willUpdateObject:atIndexPath:)]) {
NSIndexPath* newIndexPath = [_dataSource tableView:_tableView willUpdateObject:object
atIndexPath:indexPath];
if (newIndexPath) {
if (newIndexPath.length == 1) {
TTDCONDITIONLOG(TTDFLAG_TABLEVIEWMODIFICATIONS, @"UPDATING SECTION AT %@", newIndexPath);
NSInteger sectionIndex = [newIndexPath indexAtPosition:0];
[_tableView reloadSections:[NSIndexSet indexSetWithIndex:sectionIndex]
withRowAnimation:UITableViewRowAnimationTop];
} else if (newIndexPath.length == 2) {
TTDCONDITIONLOG(TTDFLAG_TABLEVIEWMODIFICATIONS, @"UPDATING ROW AT %@", newIndexPath);
[_tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:UITableViewRowAnimationTop];
}
[self invalidateView];
} else {
[_tableView reloadData];
}
}
} else {
[self refresh];
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)model:(id<TTModel>)model didInsertObject:(id)object atIndexPath:(NSIndexPath*)indexPath {
if (model == _model) {
if (_isViewAppearing && _flags.isShowingModel) {
if ([_dataSource respondsToSelector:@selector(tableView:willInsertObject:atIndexPath:)]) {
NSIndexPath* newIndexPath = [_dataSource tableView:_tableView willInsertObject:object
atIndexPath:indexPath];
if (newIndexPath) {
if (newIndexPath.length == 1) {
TTDCONDITIONLOG(TTDFLAG_TABLEVIEWMODIFICATIONS, @"INSERTING SECTION AT %@", newIndexPath);
NSInteger sectionIndex = [newIndexPath indexAtPosition:0];
[_tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]
withRowAnimation:UITableViewRowAnimationTop];
} else if (newIndexPath.length == 2) {
TTDCONDITIONLOG(TTDFLAG_TABLEVIEWMODIFICATIONS, @"INSERTING ROW AT %@", newIndexPath);
[_tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:UITableViewRowAnimationTop];
[_tableView scrollToRowAtIndexPath:newIndexPath
atScrollPosition:UITableViewScrollPositionNone animated:NO];
}
[self invalidateView];
} else {
[_tableView reloadData];
}
}
} else {
[self refresh];
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)model:(id<TTModel>)model didDeleteObject:(id)object atIndexPath:(NSIndexPath*)indexPath {
if (model == _model) {
if (_isViewAppearing && _flags.isShowingModel) {
if ([_dataSource respondsToSelector:@selector(tableView:willRemoveObject:atIndexPath:)]) {
NSIndexPath* newIndexPath = [_dataSource tableView:_tableView willRemoveObject:object
atIndexPath:indexPath];
if (newIndexPath) {
if (newIndexPath.length == 1) {
TTDCONDITIONLOG(TTDFLAG_TABLEVIEWMODIFICATIONS, @"DELETING SECTION AT %@", newIndexPath);
NSInteger sectionIndex = [newIndexPath indexAtPosition:0];
[_tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex]
withRowAnimation:UITableViewRowAnimationLeft];
} else if (newIndexPath.length == 2) {
TTDCONDITIONLOG(TTDFLAG_TABLEVIEWMODIFICATIONS, @"DELETING ROW AT %@", newIndexPath);
[_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:UITableViewRowAnimationLeft];
}
[self invalidateView];
} else {
[_tableView reloadData];
}
}
} else {
[self refresh];
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Public
///////////////////////////////////////////////////////////////////////////////////////////////////
- (UITableView*)tableView {
if (nil == _tableView) {
_tableView = [[TTTableView alloc] initWithFrame:self.view.bounds style:_tableViewStyle];
_tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth
| UIViewAutoresizingFlexibleHeight;
UIColor* backgroundColor = _tableViewStyle == UITableViewStyleGrouped
? TTSTYLEVAR(tableGroupedBackgroundColor)
: TTSTYLEVAR(tablePlainBackgroundColor);
if (backgroundColor) {
_tableView.backgroundColor = backgroundColor;
self.view.backgroundColor = backgroundColor;
}
[self.view addSubview:_tableView];
}
return _tableView;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)setTableView:(UITableView*)tableView {
if (tableView != _tableView) {
[_tableView release];
_tableView = [tableView retain];
if (!_tableView) {
self.tableBannerView = nil;
self.tableOverlayView = nil;
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)setTableBannerView:(UIView*)tableBannerView {
[self setTableBannerView:tableBannerView animated:YES];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)setTableBannerView:(UIView*)tableBannerView animated:(BOOL)animated {
TT_INVALIDATE_TIMER(_bannerTimer);
if (tableBannerView != _tableBannerView) {
if (_tableBannerView) {
if (animated) {
[self fadeOutView:_tableBannerView];
} else {
[_tableBannerView removeFromSuperview];
}
}
[_tableBannerView release];
_tableBannerView = [tableBannerView retain];
if (_tableBannerView) {
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, TTSTYLEVAR(tableBannerViewHeight), 0);
self.tableView.scrollIndicatorInsets = self.tableView.contentInset;
_tableBannerView.frame = [self rectForBannerView];
_tableBannerView.userInteractionEnabled = NO;
_tableBannerView.autoresizingMask = (UIViewAutoresizingFlexibleWidth
| UIViewAutoresizingFlexibleTopMargin);
[self addSubviewOverTableView:_tableBannerView];
if (animated) {
_tableBannerView.top += TTSTYLEVAR(tableBannerViewHeight);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:TT_TRANSITION_DURATION];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
_tableBannerView.top -= TTSTYLEVAR(tableBannerViewHeight);
[UIView commitAnimations];
}
} else {
self.tableView.contentInset = UIEdgeInsetsZero;
self.tableView.scrollIndicatorInsets = UIEdgeInsetsZero;
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)setTableOverlayView:(UIView*)tableOverlayView animated:(BOOL)animated {
if (tableOverlayView != _tableOverlayView) {
if (_tableOverlayView) {
if (animated) {
[self fadeOutView:_tableOverlayView];
} else {
[_tableOverlayView removeFromSuperview];
}
}
[_tableOverlayView release];
_tableOverlayView = [tableOverlayView retain];
if (_tableOverlayView) {
_tableOverlayView.frame = [self rectForOverlayView];
[self addToOverlayView:_tableOverlayView];
}
// XXXjoe There seem to be cases where this gets left disable - must investigate
//_tableView.scrollEnabled = !_tableOverlayView;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)setDataSource:(id<TTTableViewDataSource>)dataSource {
if (dataSource != _dataSource) {
[_dataSource release];
_dataSource = [dataSource retain];
_tableView.dataSource = nil;
self.model = dataSource.model;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)setVariableHeightRows:(BOOL)variableHeightRows {
if (variableHeightRows != _variableHeightRows) {
_variableHeightRows = variableHeightRows;
// Force the delegate to be re-created so that it supports the right kind of row measurement
_tableView.delegate = nil;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)setLoadingView:(UIView*)view {
if (view != _loadingView) {
if (_loadingView) {
[_loadingView removeFromSuperview];
TT_RELEASE_SAFELY(_loadingView);
}
_loadingView = [view retain];
if (_loadingView) {
[self addToOverlayView:_loadingView];
} else {
[self resetOverlayView];
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)setErrorView:(UIView*)view {
if (view != _errorView) {
if (_errorView) {
[_errorView removeFromSuperview];
TT_RELEASE_SAFELY(_errorView);
}
_errorView = [view retain];
if (_errorView) {
[self addToOverlayView:_errorView];
} else {
[self resetOverlayView];
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)setEmptyView:(UIView*)view {
if (view != _emptyView) {
if (_emptyView) {
[_emptyView removeFromSuperview];
TT_RELEASE_SAFELY(_emptyView);
}
_emptyView = [view retain];
if (_emptyView) {
[self addToOverlayView:_emptyView];
} else {
[self resetOverlayView];
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (id<UITableViewDelegate>)createDelegate {
if (_variableHeightRows) {
return [[[TTTableViewVarHeightDelegate alloc] initWithController:self] autorelease];
} else {
return [[[TTTableViewDelegate alloc] initWithController:self] autorelease];
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)showMenu:(UIView*)view forCell:(UITableViewCell*)cell animated:(BOOL)animated {
[self hideMenu:YES];
_menuView = [view retain];
_menuCell = [cell retain];
// Insert the cell below all content subviews
[_menuCell.contentView insertSubview:_menuView atIndex:0];
if (animated) {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:TT_FAST_TRANSITION_DURATION];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
}
// Move each content subview down, revealing the menu
for (UIView* subview in _menuCell.contentView.subviews) {
if (subview != _menuView) {
subview.left -= _menuCell.contentView.width;
}
}
if (animated) {
[UIView commitAnimations];
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)hideMenu:(BOOL)animated {
if (_menuView) {
if (animated) {
[UIView beginAnimations:nil context:_menuView];
[UIView setAnimationDuration:TT_FAST_TRANSITION_DURATION];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(hideMenuAnimationDidStop:finished:context:)];
}
for (UIView* view in _menuCell.contentView.subviews) {
if (view != _menuView) {
view.left += _menuCell.contentView.width;
}
}
if (animated) {
[UIView commitAnimations];
} else {
[_menuView removeFromSuperview];
}
TT_RELEASE_SAFELY(_menuView);
TT_RELEASE_SAFELY(_menuCell);
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)didSelectObject:(id)object atIndexPath:(NSIndexPath*)indexPath {
if ([object respondsToSelector:@selector(URLValue)]) {
NSString* URL = [object URLValue];
if (URL) {
TTOpenURL(URL);
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (BOOL)shouldOpenURL:(NSString*)URL {
return YES;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)didBeginDragging {
[self hideMenu:YES];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)didEndDragging {
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (CGRect)rectForOverlayView {
return [_tableView frameWithKeyboardSubtracted:0];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (CGRect)rectForBannerView {
CGRect tableFrame = [_tableView frameWithKeyboardSubtracted:0];
const CGFloat bannerViewHeight = TTSTYLEVAR(tableBannerViewHeight);
return CGRectMake(tableFrame.origin.x,
(tableFrame.origin.y + tableFrame.size.height) - bannerViewHeight,
tableFrame.size.width, bannerViewHeight);
}
@end