forked from twitter-archive/twui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TUITableView.m
1133 lines (956 loc) · 32.4 KB
/
TUITableView.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
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2011 Twitter, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this work except in compliance with the License.
You may obtain a copy of the License in the LICENSE file, or 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 "TUITableView.h"
#import "TUITableView+Cell.h"
#import "TUINSView.h"
typedef struct {
CGFloat offset; // from beginning of section
CGFloat height;
} TUITableViewRowInfo;
@interface TUITableViewSection : NSObject
{
TUITableView *_tableView; // weak
TUIView *_headerView; // Not reusable (similar to UITableView)
NSInteger sectionIndex;
NSUInteger numberOfRows;
CGFloat sectionHeight;
CGFloat sectionOffset;
TUITableViewRowInfo *rowInfo;
}
@property (readonly) TUIView *headerView;
@property (nonatomic, assign) CGFloat sectionOffset;
@property (readonly) NSInteger sectionIndex;
@end
@implementation TUITableViewSection
@synthesize sectionOffset;
@synthesize sectionIndex;
- (id)initWithNumberOfRows:(NSUInteger)n sectionIndex:(NSInteger)s tableView:(TUITableView *)t
{
if((self = [super init])){
_tableView = t;
sectionIndex = s;
numberOfRows = n;
rowInfo = calloc(n, sizeof(TUITableViewRowInfo));
}
return self;
}
- (void)dealloc
{
if(rowInfo) free(rowInfo);
[_headerView release];
[super dealloc];
}
- (NSUInteger)numberOfRows
{
return numberOfRows;
}
- (void)_setupRowHeights
{
sectionHeight = 0.0;
TUIView *header;
if((header = self.headerView) != nil) {
sectionHeight += roundf(header.frame.size.height);
}
for(int i = 0; i < numberOfRows; ++i) {
CGFloat h = roundf([_tableView.delegate tableView:_tableView heightForRowAtIndexPath:[TUIFastIndexPath indexPathForRow:i inSection:sectionIndex]]);
rowInfo[i].offset = sectionHeight;
rowInfo[i].height = h;
sectionHeight += h;
}
}
- (CGFloat)rowHeight:(NSInteger)i
{
if(i >= 0 && i < numberOfRows) {
return rowInfo[i].height;
}
return 0.0;
}
- (CGFloat)sectionRowOffset:(NSInteger)i
{
if(i >= 0 && i < numberOfRows){
return rowInfo[i].offset;
}
return 0.0;
}
- (CGFloat)tableRowOffset:(NSInteger)i
{
return sectionOffset + [self sectionRowOffset:i];
}
- (CGFloat)sectionHeight
{
return sectionHeight;
}
- (CGFloat)headerHeight
{
return (self.headerView != nil) ? self.headerView.frame.size.height : 0;
}
/**
* @brief Obtain the section header view.
*
* The section header view is created lazily via the data source when this
* method is first called.
*
* @return section header view
*/
- (TUIView *)headerView
{
if(_headerView == nil) {
if(_tableView.dataSource != nil && [_tableView.dataSource respondsToSelector:@selector(tableView:headerViewForSection:)]){
_headerView = [[_tableView.dataSource tableView:_tableView headerViewForSection:sectionIndex] retain];
_headerView.autoresizingMask = TUIViewAutoresizingFlexibleWidth;
}
}
return _headerView;
}
@end
@interface TUITableView (Private)
- (void)_updateDerepeaterViews;
@end
@implementation TUITableView
@synthesize pullDownView=_pullDownView;
@synthesize headerView=_headerView;
- (id)initWithFrame:(CGRect)frame style:(TUITableViewStyle)style
{
if((self = [super initWithFrame:frame])) {
_style = style;
_reusableTableCells = [[NSMutableDictionary alloc] init];
_visibleSectionHeaders = [[NSMutableIndexSet alloc] init];
_visibleItems = [[NSMutableDictionary alloc] init];
_tableFlags.animateSelectionChanges = 1;
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
return [self initWithFrame:frame style:TUITableViewStylePlain];
}
- (void)dealloc
{
[_sectionInfo release];
[_visibleSectionHeaders release];
[_visibleItems release];
[_reusableTableCells release];
[_selectedIndexPath release];
[_indexPathShouldBeFirstResponder release];
[_keepVisibleIndexPathForReload release];
[_pullDownView release];
[_dragToReorderCell release];
[_currentDragToReorderIndexPath release];
[_previousDragToReorderIndexPath release];
[_headerView release];
[super dealloc];
}
- (id<TUITableViewDelegate>)delegate
{
return (id<TUITableViewDelegate>)[super delegate];
}
- (void)setDelegate:(id<TUITableViewDelegate>)d
{
_tableFlags.delegateTableViewWillDisplayCellForRowAtIndexPath = [d respondsToSelector:@selector(tableView:willDisplayCell:forRowAtIndexPath:)];
[super setDelegate:d]; // must call super
}
- (id<TUITableViewDataSource>)dataSource
{
return _dataSource;
}
- (void)setDataSource:(id<TUITableViewDataSource>)d
{
_dataSource = d;
_tableFlags.dataSourceNumberOfSectionsInTableView = [_dataSource respondsToSelector:@selector(numberOfSectionsInTableView:)];
}
- (BOOL)animateSelectionChanges
{
return _tableFlags.animateSelectionChanges;
}
- (void)setAnimateSelectionChanges:(BOOL)a
{
_tableFlags.animateSelectionChanges = a;
}
- (NSInteger)numberOfSections
{
return [_sectionInfo count];
}
- (NSInteger)numberOfRowsInSection:(NSInteger)section
{
return [[_sectionInfo objectAtIndex:section] numberOfRows];
}
- (CGRect)rectForHeaderOfSection:(NSInteger)section {
if(section >= 0 && section < [_sectionInfo count]){
TUITableViewSection *s = [_sectionInfo objectAtIndex:section];
CGFloat offset = [s sectionOffset];
CGFloat height = [s headerHeight];
CGFloat y = _contentHeight - offset - height;
return CGRectMake(0, y, self.bounds.size.width, height);
}
return CGRectZero;
}
- (CGRect)rectForSection:(NSInteger)section
{
if(section >= 0 && section < [_sectionInfo count]){
TUITableViewSection *s = [_sectionInfo objectAtIndex:section];
CGFloat offset = [s sectionOffset];
CGFloat height = [s sectionHeight];
CGFloat y = _contentHeight - offset - height;
return CGRectMake(0, y, self.bounds.size.width, height);
}
return CGRectZero;
}
- (CGRect)rectForRowAtIndexPath:(TUIFastIndexPath *)indexPath
{
NSInteger section = indexPath.section;
NSInteger row = indexPath.row;
if(section >= 0 && section < [_sectionInfo count]) {
TUITableViewSection *s = [_sectionInfo objectAtIndex:section];
CGFloat offset = [s tableRowOffset:row];
CGFloat height = [s rowHeight:row];
CGFloat y = _contentHeight - offset - height;
return CGRectMake(0, y, self.bounds.size.width, height);
}
return CGRectZero;
}
- (NSArray *)_freshSectionInfo
{
NSInteger numberOfSections = 1;
if(_tableFlags.dataSourceNumberOfSectionsInTableView){
numberOfSections = [_dataSource numberOfSectionsInTableView:self];
}
NSMutableArray *sections = [NSMutableArray arrayWithCapacity:numberOfSections];
int s;
CGFloat offset = [_headerView bounds].size.height - self.contentInset.top;
for(s = 0; s < numberOfSections; ++s) {
TUITableViewSection *section = [[TUITableViewSection alloc] initWithNumberOfRows:[_dataSource tableView:self numberOfRowsInSection:s] sectionIndex:s tableView:self];
[section _setupRowHeights];
section.sectionOffset = offset;
offset += [section sectionHeight];
[sections addObject:section];
[section release];
}
_contentHeight = offset - self.contentInset.bottom;
return sections;
}
- (void)_enqueueReusableCell:(TUITableViewCell *)cell
{
NSString *identifier = cell.reuseIdentifier;
if(!identifier)
return;
NSMutableArray *array = [_reusableTableCells objectForKey:identifier];
if(!array) {
array = [[NSMutableArray alloc] init];
[_reusableTableCells setObject:array forKey:identifier];
[array release];
}
[array addObject:cell];
}
- (TUITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier
{
if(!identifier)
return nil;
NSMutableArray *array = [_reusableTableCells objectForKey:identifier];
if(array) {
TUITableViewCell *c = [array lastObject];
if(c) {
[c retain];
[array removeLastObject];
[c prepareForReuse];
return [c autorelease];
}
}
return nil;
}
/**
* @brief Obtain the header view for the specified section
*
* If the section has no header, nil is returned.
*
* @param section the section
* @return section header
*/
- (TUIView *)headerViewForSection:(NSInteger)section {
if(section >= 0 && section < [_sectionInfo count]){
return [(TUITableViewSection *)[_sectionInfo objectAtIndex:section] headerView];
}else{
return nil;
}
}
- (TUITableViewCell *)cellForRowAtIndexPath:(TUIFastIndexPath *)indexPath // returns nil if cell is not visible or index path is out of range
{
return [_visibleItems objectForKey:indexPath];
}
- (NSArray *)visibleCells
{
return [_visibleItems allValues];
}
static NSInteger SortCells(TUITableViewCell *a, TUITableViewCell *b, void *ctx)
{
if(a.frame.origin.y > b.frame.origin.y)
return NSOrderedAscending;
return NSOrderedDescending;
}
- (NSArray *)sortedVisibleCells
{
NSArray *v = [self visibleCells];
return [v sortedArrayUsingComparator:(NSComparator)^NSComparisonResult(TUITableViewCell *a, TUITableViewCell *b) {
if(a.frame.origin.y > b.frame.origin.y)
return NSOrderedAscending;
return NSOrderedDescending;
}];
}
#define INDEX_PATHS_FOR_VISIBLE_ROWS [_visibleItems allKeys]
- (NSArray *)indexPathsForVisibleRows
{
return INDEX_PATHS_FOR_VISIBLE_ROWS;
}
- (TUIFastIndexPath *)indexPathForCell:(TUITableViewCell *)c
{
for(TUIFastIndexPath *i in _visibleItems) {
TUITableViewCell *cell = [_visibleItems objectForKey:i];
if(cell == c)
return i;
}
return nil;
}
/**
* @brief Obtain the indexes of sections which intersect @p rect.
*
* @param rect the rect
* @return intersecting sections
*/
- (NSIndexSet *)indexesOfSectionsInRect:(CGRect)rect
{
NSMutableIndexSet *indexes = [[NSMutableIndexSet alloc] init];
for(int i = 0; i < [_sectionInfo count]; i++) {
if(CGRectIntersectsRect([self rectForSection:i], rect)){
[indexes addIndex:i];
}
}
return [indexes autorelease];
}
/**
* @brief Obtain the indexes of sections whose header views intersect @p rect.
*
* @param rect the rect
* @return intersecting sections
*/
- (NSIndexSet *)indexesOfSectionHeadersInRect:(CGRect)rect
{
NSMutableIndexSet *indexes = [[NSMutableIndexSet alloc] init];
for(int i = 0; i < [_sectionInfo count]; i++) {
if(CGRectIntersectsRect([self rectForHeaderOfSection:i], rect)){
[indexes addIndex:i];
}
}
return [indexes autorelease];
}
- (NSArray *)indexPathsForRowsInRect:(CGRect)rect
{
NSMutableArray *indexPaths = [NSMutableArray arrayWithCapacity:50];
NSInteger sectionIndex = 0;
for(TUITableViewSection *section in _sectionInfo) {
NSInteger numberOfRows = [section numberOfRows];
for(NSInteger row = 0; row < numberOfRows; ++row) {
TUIFastIndexPath *indexPath = [TUIFastIndexPath indexPathForRow:row inSection:sectionIndex];
CGRect cellRect = [self rectForRowAtIndexPath:indexPath];
if(CGRectIntersectsRect(cellRect, rect)) {
[indexPaths addObject:indexPath];
} else {
// not visible
}
}
++sectionIndex;
}
return indexPaths;
}
/**
* @brief Obtain the index path of the row at the specified point
*
* If the point is not valid or no row exists at that point, nil is
* returned.
*
* @param point location in the table view
* @return index path of the row at @p point
*/
- (TUIFastIndexPath *)indexPathForRowAtPoint:(CGPoint)point {
NSInteger sectionIndex = 0;
for(TUITableViewSection *section in _sectionInfo){
for(NSInteger row = 0; row < [section numberOfRows]; row++){
TUIFastIndexPath *indexPath = [TUIFastIndexPath indexPathForRow:row inSection:sectionIndex];
CGRect cellRect = [self rectForRowAtIndexPath:indexPath];
if(CGRectContainsPoint(cellRect, point)){
return indexPath;
}
}
++sectionIndex;
}
return nil;
}
/**
* @brief Obtain the index of a section whose header is at the specified point
*
* If the point is not valid or no header exists at that point, a negative value
* is returned.
*
* @param point location in the table view
* @return index of the section whose header is at @p point
*/
- (NSInteger)indexOfSectionWithHeaderAtPoint:(CGPoint)point {
NSInteger sectionIndex = 0;
for(TUITableViewSection *section in _sectionInfo){
TUIView *headerView;
if((headerView = section.headerView) != nil){
CGFloat offset = [section sectionOffset];
CGFloat height = [section headerHeight];
CGFloat y = _contentHeight - offset - height;
CGRect frame = CGRectMake(0, y, self.bounds.size.width, height);
if(CGRectContainsPoint(frame, point)){
return sectionIndex;
}
}
sectionIndex++;
}
return -1;
}
/**
* @brief Enumerate index paths
* @see #enumerateIndexPathsFromIndexPath:toIndexPath:withOptions:usingBlock:
*/
- (void)enumerateIndexPathsUsingBlock:(void (^)(TUIFastIndexPath *indexPath, BOOL *stop))block {
[self enumerateIndexPathsFromIndexPath:nil toIndexPath:nil withOptions:0 usingBlock:block];
}
/**
* @brief Enumerate index paths
* @see #enumerateIndexPathsFromIndexPath:toIndexPath:withOptions:usingBlock:
*/
- (void)enumerateIndexPathsWithOptions:(NSEnumerationOptions)options usingBlock:(void (^)(TUIFastIndexPath *indexPath, BOOL *stop))block {
[self enumerateIndexPathsFromIndexPath:nil toIndexPath:nil withOptions:options usingBlock:block];
}
/**
* @brief Enumerate index paths
*
* The provided block is repeatedly invoked with each valid index path between
* the specified bounds. Both bounding index paths are inclusive.
*
* @param fromIndexPath the index path to begin enumerating at or nil to begin at the first index path
* @param toIndexPath the index path to stop enumerating at or nil to stop at the last index path
* @param options enumeration options (not currently supported; pass 0)
* @param block the block to enumerate with
*/
- (void)enumerateIndexPathsFromIndexPath:(TUIFastIndexPath *)fromIndexPath toIndexPath:(TUIFastIndexPath *)toIndexPath withOptions:(NSEnumerationOptions)options usingBlock:(void (^)(TUIFastIndexPath *indexPath, BOOL *stop))block {
NSInteger sectionLowerBound = (fromIndexPath != nil) ? fromIndexPath.section : 0;
NSInteger sectionUpperBound = (toIndexPath != nil) ? toIndexPath.section : [self numberOfSections] - 1;
NSInteger rowLowerBound = (fromIndexPath != nil) ? fromIndexPath.row : 0;
NSInteger rowUpperBound = (toIndexPath != nil) ? toIndexPath.row : -1;
NSInteger irow = rowLowerBound; // start at the lower bound row for the first iteration...
for(NSInteger i = sectionLowerBound; i < [self numberOfSections] && i <= sectionUpperBound /* inclusive */; i++){
NSInteger rowCount = [self numberOfRowsInSection:i];
for(NSInteger j = irow; j < rowCount && j <= ((rowUpperBound < 0 || i < sectionUpperBound) ? rowCount - 1 : rowUpperBound) /* inclusive */; j++){
BOOL stop = FALSE;
block([TUIFastIndexPath indexPathForRow:j inSection:i], &stop);
if(stop) return;
}
irow = 0; // ...then use zero for subsequent iterations
}
}
- (TUIFastIndexPath *)_topVisibleIndexPath
{
TUIFastIndexPath *topVisibleIndex = nil;
NSArray *v = [INDEX_PATHS_FOR_VISIBLE_ROWS sortedArrayUsingSelector:@selector(compare:)];
if([v count])
topVisibleIndex = [v objectAtIndex:0];
return topVisibleIndex;
}
- (void)setFrame:(CGRect)f
{
_tableFlags.forceSaveScrollPosition = 1;
[super setFrame:f];
}
- (void)setContentOffset:(CGPoint)p
{
_tableFlags.didFirstLayout = 1; // prevent the auto-scroll-to-top during the first layout
[super setContentOffset:p];
// if we're currently dragging we need to update the drag operation since the content under
// the mouse has moved; we just call update again with the last mouse location
if([self __isDraggingCell]){
[self __updateDraggingCell:_dragToReorderCell offset:_currentDragToReorderMouseOffset location:_currentDragToReorderLocation];
}
}
- (void)setPullDownView:(TUIView *)p
{
[_pullDownView removeFromSuperview];
[p retain];
[_pullDownView release];
_pullDownView = p;
[self addSubview:_pullDownView];
_pullDownView.hidden = YES;
}
- (void)setHeaderView:(TUIView *)h
{
[_headerView removeFromSuperview];
[h retain];
[_headerView release];
_headerView = h;
[self addSubview:_headerView];
_headerView.hidden = YES;
}
- (BOOL)_preLayoutCells
{
CGRect bounds = self.bounds;
if(!_sectionInfo || !CGSizeEqualToSize(bounds.size, _lastSize)) {
// save scroll position
CGFloat previousOffset = 0.0f;
TUIFastIndexPath *savedIndexPath = nil;
CGFloat relativeOffset = 0.0;
if(_tableFlags.maintainContentOffsetAfterReload) {
previousOffset = self.contentSize.height + self.contentOffset.y;
} else {
if(_tableFlags.forceSaveScrollPosition || [self.nsView inLiveResize]) {
_tableFlags.forceSaveScrollPosition = 0;
NSArray *a = [INDEX_PATHS_FOR_VISIBLE_ROWS sortedArrayUsingSelector:@selector(compare:)];
if([a count]) {
savedIndexPath = [[a objectAtIndex:0] retain];
CGRect v = [self visibleRect];
CGRect r = [self rectForRowAtIndexPath:savedIndexPath];
relativeOffset = ((v.origin.y + v.size.height) - (r.origin.y + r.size.height));
relativeOffset += (_lastSize.height - bounds.size.height);
}
} else if(_keepVisibleIndexPathForReload) {
savedIndexPath = [_keepVisibleIndexPathForReload retain];
relativeOffset = _relativeOffsetForReload;
[_keepVisibleIndexPathForReload release];
_keepVisibleIndexPathForReload = nil;
}
}
[_sectionInfo release];
_sectionInfo = [[self _freshSectionInfo] retain]; // calculates new contentHeight here
self.contentSize = CGSizeMake(self.bounds.size.width, _contentHeight);
_lastSize = bounds.size;
if(!_tableFlags.didFirstLayout) {
_tableFlags.didFirstLayout = 1;
[self scrollToTopAnimated:NO];
}
// restore scroll position
if(_tableFlags.maintainContentOffsetAfterReload) {
CGFloat newOffset = previousOffset - self.contentSize.height;
self.contentOffset = CGPointMake(self.contentOffset.x, newOffset);
} else {
if(savedIndexPath) {
CGRect v = [self visibleRect];
CGRect r = [self rectForRowAtIndexPath:savedIndexPath];
r.origin.y -= (v.size.height - r.size.height);
r.size.height += (v.size.height - r.size.height);
r.origin.y += relativeOffset;
[self scrollRectToVisible:r animated:NO];
[savedIndexPath release];
}
}
return YES; // needs visible cells to be redisplayed
}
return NO; // just need to do the recycling
}
/**
* @brief Layout header views for sections which have one.
*/
- (void)_layoutSectionHeaders:(BOOL)visibleHeadersNeedRelayout
{
if(visibleHeadersNeedRelayout) {
if(_visibleSectionHeaders != nil){
[_visibleSectionHeaders enumerateIndexesUsingBlock:^(NSUInteger index, BOOL *stop) {
if(index < [_sectionInfo count]) {
TUITableViewSection *section = [_sectionInfo objectAtIndex:index];
if(section.headerView != nil) {
section.headerView.frame = [self rectForHeaderOfSection:index];
[section.headerView setNeedsLayout];
}
}
}];
}
}
CGRect visible = [self visibleRect];
NSIndexSet *oldIndexes = _visibleSectionHeaders;
NSIndexSet *newIndexes = [self indexesOfSectionHeadersInRect:visible];
NSMutableIndexSet *toRemove = [[oldIndexes mutableCopy] autorelease];
[toRemove removeIndexes:newIndexes];
NSMutableIndexSet *toAdd = [[newIndexes mutableCopy] autorelease];
[toAdd removeIndexes:oldIndexes];
// remove offscreen headers
[toRemove enumerateIndexesUsingBlock:^(NSUInteger index, BOOL *stop) {
if(index < [_sectionInfo count]) {
TUITableViewSection *section = [_sectionInfo objectAtIndex:index];
if(section.headerView != nil) {
[section.headerView removeFromSuperview];
}
}
[_visibleSectionHeaders removeIndex:index];
}];
// add new headers
[toAdd enumerateIndexesUsingBlock:^(NSUInteger index, BOOL *stop) {
if(index < [_sectionInfo count]){
TUITableViewSection *section = [_sectionInfo objectAtIndex:index];
if(section.headerView != nil) {
section.headerView.frame = [self rectForHeaderOfSection:index];
[section.headerView setNeedsLayout];
[self addSubview:section.headerView];
}
}
[_visibleSectionHeaders addIndex:index];
}];
}
- (void)_layoutCells:(BOOL)visibleCellsNeedRelayout
{
if(visibleCellsNeedRelayout) {
// update remaining visible cells if needed
for(TUIFastIndexPath *i in _visibleItems) {
TUITableViewCell *cell = [_visibleItems objectForKey:i];
cell.frame = [self rectForRowAtIndexPath:i];
[cell setNeedsLayout];
}
}
CGRect visible = [self visibleRect];
// Example:
// old: 0 1 2 3 4 5 6 7
// new: 2 3 4 5 6 7 8 9
// to remove: 0 1
// to add: 8 9
NSArray *oldVisibleIndexPaths = INDEX_PATHS_FOR_VISIBLE_ROWS;
NSArray *newVisibleIndexPaths = [self indexPathsForRowsInRect:visible];
NSMutableArray *indexPathsToRemove = [[oldVisibleIndexPaths mutableCopy] autorelease];
[indexPathsToRemove removeObjectsInArray:newVisibleIndexPaths];
NSMutableArray *indexPathsToAdd = [[newVisibleIndexPaths mutableCopy] autorelease];
[indexPathsToAdd removeObjectsInArray:oldVisibleIndexPaths];
// remove offscreen cells
for(TUIFastIndexPath *i in indexPathsToRemove) {
TUITableViewCell *cell = [self cellForRowAtIndexPath:i];
// don't reuse the dragged cell
if(_dragToReorderCell == nil || ![cell isEqual:_dragToReorderCell]){
[self _enqueueReusableCell:cell];
[cell removeFromSuperview];
[_visibleItems removeObjectForKey:i];
}
}
// add new cells
for(TUIFastIndexPath *i in indexPathsToAdd) {
if([_visibleItems objectForKey:i]) {
NSLog(@"!!! Warning: already have a cell in place for index path %@\n\n\n", i);
} else {
TUITableViewCell *cell = [_dataSource tableView:self cellForRowAtIndexPath:i];
[self.nsView invalidateHoverForView:cell];
cell.frame = [self rectForRowAtIndexPath:i];
[cell setNeedsLayout];
[cell prepareForDisplay];
if([i isEqual:_selectedIndexPath]) {
[cell setSelected:YES animated:NO];
} else {
[cell setSelected:NO animated:NO];
}
[self addSubview:cell];
if([_indexPathShouldBeFirstResponder isEqual:i]) {
// only make cells first responder if they accept it
if([cell acceptsFirstResponder]){
[self.nsWindow makeFirstResponderIfNotAlreadyInResponderChain:cell withFutureRequestToken:_futureMakeFirstResponderToken];
}
[_indexPathShouldBeFirstResponder release];
_indexPathShouldBeFirstResponder = nil;
}
[_visibleItems setObject:cell forKey:i];
}
}
// if we have a dragged cell, make sure it's on top of the newly added cells
if([indexPathsToAdd count] > 0 && _dragToReorderCell != nil){
[[_dragToReorderCell superview] bringSubviewToFront:_dragToReorderCell];
}
if(_headerView) {
CGSize s = self.contentSize;
CGRect headerViewRect = CGRectMake(0, s.height - _headerView.frame.size.height, visible.size.width, _headerView.frame.size.height);
if(CGRectIntersectsRect(headerViewRect, visible)) {
_headerView.frame = headerViewRect;
if(_headerView.hidden) {
// show
_headerView.hidden = NO;
}
} else {
if(!_headerView.hidden) {
_headerView.hidden = YES;
}
}
}
if(_pullDownView) {
CGSize s = self.contentSize;
CGRect pullDownRect = CGRectMake(0, s.height, visible.size.width, _pullDownView.frame.size.height);
if([self pullDownViewIsVisible]) {
if(_pullDownView.hidden) {
// show
_pullDownView.frame = pullDownRect;
_pullDownView.hidden = NO;
}
} else {
if(!_pullDownView.hidden) {
_pullDownView.hidden = YES;
}
}
}
}
- (BOOL)pullDownViewIsVisible
{
if(_pullDownView) {
CGSize s = self.contentSize;
CGRect visible = [self visibleRect];
CGRect pullDownRect = CGRectMake(0, s.height, visible.size.width, _pullDownView.frame.size.height);
return CGRectIntersectsRect(pullDownRect, visible);
}
return NO;
}
- (void)reloadDataMaintainingVisibleIndexPath:(TUIFastIndexPath *)indexPath relativeOffset:(CGFloat)relativeOffset
{
[_keepVisibleIndexPathForReload release];
_keepVisibleIndexPathForReload = [indexPath retain];
_relativeOffsetForReload = relativeOffset;
[self reloadData];
}
- (void)reloadData
{
// notify our delegate we're about to reload the table
if(self.delegate != nil && [self.delegate respondsToSelector:@selector(tableViewWillReloadData:)]){
[self.delegate tableViewWillReloadData:self];
}
// need to recycle all visible cells, have them be regenerated on layoutSubviews
// because the same cells might have different content
for(TUIFastIndexPath *i in _visibleItems) {
TUITableViewCell *cell = [_visibleItems objectForKey:i];
[self _enqueueReusableCell:cell];
[cell removeFromSuperview];
}
// if we have a dragged cell, clear it
[_dragToReorderCell release];
_dragToReorderCell = nil;
// clear visible cells
[_visibleItems removeAllObjects];
// remove any visible headers, they should be re-added when the table is laid out
for(TUITableViewSection *section in _sectionInfo){
TUIView *headerView;
if((headerView = [section headerView]) != nil){
[headerView removeFromSuperview];
}
}
// clear visible section headers
[_visibleSectionHeaders removeAllIndexes];
[_sectionInfo release];
_sectionInfo = nil; // will be regenerated on next layout
[self layoutSubviews];
// notify our delegate the table view has been reloaded
if(self.delegate != nil && [self.delegate respondsToSelector:@selector(tableViewDidReloadData:)]){
[self.delegate tableViewDidReloadData:self];
}
}
- (void)layoutSubviews
{
if(!_tableFlags.layoutSubviewsReentrancyGuard) {
_tableFlags.layoutSubviewsReentrancyGuard = 1;
[TUIView setAnimationsEnabled:NO block:^{
[CATransaction begin];
[CATransaction setDisableActions:YES];
BOOL visibleCellsNeedRelayout = [self _preLayoutCells];
[super layoutSubviews]; // this will munge with the contentOffset
[self _layoutSectionHeaders:visibleCellsNeedRelayout];
[self _layoutCells:visibleCellsNeedRelayout];
if(_tableFlags.derepeaterEnabled)
[self _updateDerepeaterViews];
[CATransaction commit];
}];
_tableFlags.layoutSubviewsReentrancyGuard = 0;
} else {
// NSLog(@"trying to nest...");
}
}
- (void)scrollToRowAtIndexPath:(TUIFastIndexPath *)indexPath atScrollPosition:(TUITableViewScrollPosition)scrollPosition animated:(BOOL)animated
{
CGRect v = [self visibleRect];
CGRect r = [self rectForRowAtIndexPath:indexPath];
switch(scrollPosition) {
case TUITableViewScrollPositionNone:
// do nothing
break;
case TUITableViewScrollPositionTop:
r.origin.y -= (v.size.height - r.size.height);
r.size.height += (v.size.height - r.size.height);
[self scrollRectToVisible:r animated:animated];
break;
case TUITableViewScrollPositionToVisible:
default:
[self scrollRectToVisible:r animated:animated];
break;
}
}
- (TUIFastIndexPath *)indexPathForSelectedRow
{
return _selectedIndexPath;
}
- (TUIFastIndexPath *)indexPathForFirstRow
{
return [TUIFastIndexPath indexPathForRow:0 inSection:0];
}
- (TUIFastIndexPath *)indexPathForLastRow
{
NSInteger sec = [self numberOfSections] - 1;
NSInteger row = [self numberOfRowsInSection:sec] - 1;
return [TUIFastIndexPath indexPathForRow:row inSection:sec];
}
- (void)_makeRowAtIndexPathFirstResponder:(TUIFastIndexPath *)indexPath
{
TUITableViewCell *cell = [self cellForRowAtIndexPath:indexPath];
// only cells that accept first responder should be made first responder
if(cell && [cell acceptsFirstResponder]) {
[self.nsWindow makeFirstResponderIfNotAlreadyInResponderChain:cell];
} else {
[_indexPathShouldBeFirstResponder release];
_indexPathShouldBeFirstResponder = [indexPath retain];
_futureMakeFirstResponderToken = [self.nsWindow futureMakeFirstResponderRequestToken];
}
}
- (void)selectRowAtIndexPath:(TUIFastIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(TUITableViewScrollPosition)scrollPosition
{
if([indexPath isEqual:[self indexPathForSelectedRow]]) {
// just scroll to visible
} else {
[self deselectRowAtIndexPath:[self indexPathForSelectedRow] animated:animated];
TUITableViewCell *cell = [self cellForRowAtIndexPath:indexPath]; // may be nil
[cell setSelected:YES animated:animated];
[_selectedIndexPath release]; // should already be nil
_selectedIndexPath = [indexPath retain];
[cell setNeedsDisplay];
// only notify when the selection actually changes
if([self.delegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)]){
[self.delegate tableView:self didSelectRowAtIndexPath:indexPath];
}
}
[self _makeRowAtIndexPathFirstResponder:indexPath];
[self scrollToRowAtIndexPath:indexPath atScrollPosition:scrollPosition animated:animated];
}
- (void)deselectRowAtIndexPath:(TUIFastIndexPath *)indexPath animated:(BOOL)animated
{
if([indexPath isEqual:_selectedIndexPath]) {
TUITableViewCell *cell = [self cellForRowAtIndexPath:indexPath]; // may be nil
[cell setSelected:NO animated:animated];
[_selectedIndexPath release];
_selectedIndexPath = nil;
[cell setNeedsDisplay];