forked from danielctull/DTGridView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DTGridView.m
924 lines (661 loc) · 27.4 KB
/
DTGridView.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
//
// DTGridView.m
// GridViewTester
//
// Created by Daniel Tull on 05.12.2008.
// Copyright 2008 Daniel Tull. All rights reserved.
//
#import "DTGridView.h"
#import "DTGridViewCellInfoProtocol.h"
NSInteger const DTGridViewInvalid = -1;
NSInteger intSort(id info1, id info2, void *context);
@interface DTGridViewCellInfo : NSObject <DTGridViewCellInfoProtocol> {
NSUInteger xPosition, yPosition;
CGRect frame;
CGFloat x, y, width, height;
}
@property (nonatomic, assign) CGFloat x, y, width, height;
@end
@implementation DTGridViewCellInfo
@synthesize xPosition, yPosition, x, y, width, height, frame;
- (NSString *)description {
return [NSString stringWithFormat:@"DTGridViewCellInfo: frame=(%i %i; %i %i) x=%i, y=%i", (NSInteger)self.frame.origin.x, (NSInteger)self.frame.origin.y, (NSInteger)self.frame.size.width, (NSInteger)self.frame.size.height, self.xPosition, self.yPosition];
}
@end
@interface DTGridView ()
- (void)dctInternal_setupInternals;
- (void)loadData;
- (void)checkViews;
- (void)initialiseViews;
- (void)fireEdgeScroll;
- (void)checkNewRowStartingWithCellInfo:(NSObject<DTGridViewCellInfoProtocol> *)info goingUp:(BOOL)goingUp;
- (NSObject<DTGridViewCellInfoProtocol> *)cellInfoForRow:(NSUInteger)row column:(NSUInteger)col;
- (void)checkRow:(NSInteger)row column:(NSInteger)col goingLeft:(BOOL)goingLeft;
- (void)decelerationTimer:(NSTimer *)timer;
- (void)draggingTimer:(NSTimer *)timer;
@property (nonatomic, retain) NSTimer *decelerationTimer, *draggingTimer;
@end
@implementation DTGridView
@dynamic delegate;
@synthesize dataSource, gridCells, numberOfRows, cellOffset, outset;
@synthesize decelerationTimer, draggingTimer, layoutDirectionality;
- (void)dealloc {
super.delegate = nil;
self.dataSource = nil;
[cellsOnScreen release], cellsOnScreen = nil;
[gridRows release], gridRows = nil;
[rowPositions release], rowPositions = nil;
[rowHeights release], rowHeights = nil;
[freeCells release], freeCells = nil;
[cellInfoForCellsOnScreen release], cellInfoForCellsOnScreen = nil;
[super dealloc];
}
- (void)setGridDelegate:(id <DTGridViewDelegate>)aDelegate {
self.delegate = aDelegate;
}
- (id <DTGridViewDelegate>)gridDelegate {
return self.delegate;
}
NSInteger intSort(id info1, id info2, void *context) {
DTGridViewCellInfo *i1 = (DTGridViewCellInfo *)info1;
DTGridViewCellInfo *i2 = (DTGridViewCellInfo *)info2;
if (i1.yPosition < i2.yPosition)
return NSOrderedAscending;
else if (i1.yPosition > i2.yPosition)
return NSOrderedDescending;
else if (i1.xPosition < i2.xPosition)
return NSOrderedAscending;
else if (i1.xPosition > i2.xPosition)
return NSOrderedDescending;
else
return NSOrderedSame;
}
- (id)init {
if (!(self = [super init])) return nil;
[self dctInternal_setupInternals];
return self;
}
- (id)initWithFrame:(CGRect)frame {
if (!(self = [super initWithFrame:frame])) return nil;
[self dctInternal_setupInternals];
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
if (!(self = [super initWithCoder:aDecoder])) return nil;
[self dctInternal_setupInternals];
return self;
}
- (void)dctInternal_setupInternals {
numberOfRows = DTGridViewInvalid;
columnIndexOfSelectedCell = DTGridViewInvalid;
rowIndexOfSelectedCell = DTGridViewInvalid;
layoutDirectionality = DTGridViewDirectionalityDefault;
gridRows = [[NSMutableArray alloc] init];
rowPositions = [[NSMutableArray alloc] init];
rowHeights = [[NSMutableArray alloc] init];
cellsOnScreen = [[NSMutableArray alloc] init];
freeCells = [[NSMutableArray alloc] init];
cellInfoForCellsOnScreen = [[NSMutableArray alloc] init];
}
- (void)setFrame:(CGRect)aFrame {
CGSize oldSize = self.frame.size;
CGSize newSize = aFrame.size;
if (oldSize.height != newSize.height || oldSize.width != newSize.width) {
hasResized = YES;
}
[super setFrame:aFrame];
if (hasResized) {
[self setNeedsLayout];
}
}
- (void)reloadData {
[self loadData];
[self setNeedsDisplay];
[self setNeedsLayout];
}
- (void)drawRect:(CGRect)rect {
oldContentOffset = CGPointMake(0.0f, 0.0f);
//hasLoadedData = NO;
//if (!hasLoadedData)
[self loadData];
for (UIView *v in self.subviews)
if ([v isKindOfClass:[DTGridViewCell class]])
[v removeFromSuperview];
[self initialiseViews];
[self didLoad];
}
- (void)didLoad {
if ([self.delegate respondsToSelector:@selector(gridViewDidLoad:)])
[self.delegate gridViewDidLoad:self];
}
- (void)didEndDragging {}
- (void)didEndDecelerating {}
- (void)didEndMoving {}
- (void)layoutSubviews {
if (self.contentSize.width > 0.0 || self.contentSize.height > 0.0){
dispatch_once(&driftOnceToken, ^{
//Set the offset according to the directionality
CGPoint newContentOffset = self.contentOffset;
if (self.layoutDirectionality & DTGridViewDirectionalityRightToLeft){
newContentOffset.x += ([self realContentSize].width - self.frame.size.width);
}
if (self.layoutDirectionality & DTGridViewDirectionalityBottomToTop){
newContentOffset.y += ([self realContentSize].height - self.frame.size.height);
}
self.contentOffset = newContentOffset;
});
}
[super layoutSubviews];
[self checkViews];
[self fireEdgeScroll];
if (!self.draggingTimer && !self.decelerationTimer && self.dragging)
self.draggingTimer = [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(draggingTimer:) userInfo:nil repeats:NO];
if (!self.decelerationTimer && self.decelerating) {
self.decelerationTimer = [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(decelerationTimer:) userInfo:nil repeats:NO];
[self.draggingTimer invalidate];
self.draggingTimer = nil;
}
}
- (void)decelerationTimer:(NSTimer *)timer {
self.decelerationTimer = nil;
[self didEndDecelerating];
[self didEndMoving];
}
- (void)draggingTimer:(NSTimer *)timer {
self.draggingTimer = nil;
[self didEndDragging];
[self didEndMoving];
}
#pragma mark Adding and Removing Cells
- (void)addCellWithInfo:(NSObject<DTGridViewCellInfoProtocol> *)info {
if (![info isMemberOfClass:[DTGridViewCellInfo class]]) return;
[cellInfoForCellsOnScreen addObject:info];
[cellInfoForCellsOnScreen sortUsingFunction:intSort context:NULL];
DTGridViewCell *cell = [[self findViewForRow:info.yPosition column:info.xPosition] retain];
[cell setNeedsDisplay];
cell.xPosition = info.xPosition;
cell.yPosition = info.yPosition;
cell.delegate = self;
cell.frame = info.frame;
if (cell.xPosition == columnIndexOfSelectedCell && cell.yPosition == rowIndexOfSelectedCell)
cell.selected = YES;
else
cell.selected = NO;
[[gridCells objectAtIndex:info.yPosition] replaceObjectAtIndex:info.xPosition withObject:cell];
[self insertSubview:cell atIndex:0];
// remove any existing view at this frame
for (UIView *v in self.subviews) {
if ([v isKindOfClass:[DTGridViewCell class]] &&
v.frame.origin.x == cell.frame.origin.x &&
v.frame.origin.y == cell.frame.origin.y &&
v != cell) {
[v removeFromSuperview];
break;
}
}
[cell release];
}
- (void)removeCellWithInfo:(DTGridViewCellInfo *)info {
if (info.yPosition >= [gridCells count]) return;
NSMutableArray *row = [gridCells objectAtIndex:info.yPosition];
if (info.xPosition >= [row count]) return;
DTGridViewCell *cell = [row objectAtIndex:info.xPosition];
if (![cell isKindOfClass:[DTGridViewCell class]]) return;
[cell retain];
[cell removeFromSuperview];
[row replaceObjectAtIndex:info.xPosition withObject:info];
[cellInfoForCellsOnScreen removeObject:info];
// TODO: Should this be set?
//cell.frame = CGRectZero;
[freeCells addObject:cell];
[cell release];
}
- (CGRect)visibleRect {
CGRect visibleRect;
visibleRect.origin = self.contentOffset;
visibleRect.size = self.bounds.size;
return visibleRect;
}
- (BOOL)rowOfCellInfoShouldBeOnShow:(NSObject<DTGridViewCellInfoProtocol> *)info {
CGRect visibleRect = [self visibleRect];
CGRect infoFrame = info.frame;
CGFloat infoBottom = infoFrame.origin.y + infoFrame.size.height;
CGFloat infoTop = infoFrame.origin.y;
CGFloat visibleBottom = visibleRect.origin.y + visibleRect.size.height;
CGFloat visibleTop = visibleRect.origin.y;
return (infoBottom >= visibleTop &&
infoTop <= visibleBottom);
}
- (BOOL)cellInfoShouldBeOnShow:(NSObject<DTGridViewCellInfoProtocol> *)info {
if (!info || ![info isMemberOfClass:[DTGridViewCellInfo class]]) return NO;
CGRect visibleRect = [self visibleRect];
CGFloat infoRight = info.frame.origin.x + info.frame.size.width;
CGFloat infoLeft = info.frame.origin.x;
CGFloat visibleRight = visibleRect.origin.x + visibleRect.size.width;
CGFloat visibleLeft = visibleRect.origin.x;
if (infoRight >= visibleLeft &&
infoLeft <= visibleRight &&
[self rowOfCellInfoShouldBeOnShow:info]) return YES;
//NSLog(@"%@ NO: %@", NSStringFromSelector(_cmd), NSStringFromCGRect(info.frame));
return NO;
}
#pragma mark -
#pragma mark Finding Infomation from DataSource
- (CGFloat)findWidthForRow:(NSInteger)row column:(NSInteger)column {
if (self.layoutDirectionality & DTGridViewDirectionalityBottomToTop){
//Flip rows if needed
row = [self findNumberOfRows] - row;
}
if (self.layoutDirectionality & DTGridViewDirectionalityRightToLeft){
//Flip columns if needed
column = [self findNumberOfColumnsForRow:row] - column - 1;
}
return [self.dataSource gridView:self widthForCellAtRow:row column:column];
}
- (NSInteger)findNumberOfRows {
return [self.dataSource numberOfRowsInGridView:self];
}
- (NSInteger)findNumberOfColumnsForRow:(NSInteger)row {
if (self.layoutDirectionality & DTGridViewDirectionalityBottomToTop){
//Flip rows if needed
row = [self findNumberOfRows] - row;
}
return [self.dataSource numberOfColumnsInGridView:self forRowWithIndex:row];
}
- (CGFloat)findHeightForRow:(NSInteger)row {
if (self.layoutDirectionality & DTGridViewDirectionalityBottomToTop){
//Flip rows if needed
row = [self findNumberOfRows] - row;
}
return [self.dataSource gridView:self heightForRow:row];
}
- (DTGridViewCell *)findViewForRow:(NSInteger)row column:(NSInteger)column {
if (self.layoutDirectionality & DTGridViewDirectionalityBottomToTop){
//Flip rows if needed
row = [self findNumberOfRows] - row;
}
if (self.layoutDirectionality & DTGridViewDirectionalityRightToLeft){
//Flip columns if needed
column = [self findNumberOfColumnsForRow:row] - column - 1;
}
return [self.dataSource gridView:self viewForRow:row column:column];
}
- (CGSize)realContentSize{
return self.contentSize;
}
#pragma mark -
- (void)loadData {
hasLoadedData = YES;
if (![self.dataSource respondsToSelector:@selector(numberOfRowsInGridView:)])
return;
self.numberOfRows = [self findNumberOfRows];
if (!self.numberOfRows)
return;
[gridRows removeAllObjects];
[rowHeights removeAllObjects];
[rowPositions removeAllObjects];
NSMutableArray *cellInfoArrayRows = [[NSMutableArray alloc] init];
CGFloat maxHeight = 0;
CGFloat maxWidth = 0;
for (NSInteger i = 0; i < self.numberOfRows; i++) {
NSInteger numberOfCols = [self findNumberOfColumnsForRow:i];
NSMutableArray *cellInfoArrayCols = [[NSMutableArray alloc] init];
for (NSInteger j = 0; j < numberOfCols; j++) {
DTGridViewCellInfo *info = [[DTGridViewCellInfo alloc] init];
info.xPosition = j;
info.yPosition = i;
CGFloat height = [self findHeightForRow:i];
CGFloat width = [self findWidthForRow:i column:j];
//info.frame.size.height = [dataSource gridView:self heightForRow:i];
//info.frame.size.width = [dataSource gridView:self widthForCellAtRow:i column:j];
CGFloat y;
CGFloat x;
if (i == 0) {
y = 0.0f;
//info.frame.origin.y = 0.0;
} else {
DTGridViewCellInfo *previousCellRow = [[cellInfoArrayRows objectAtIndex:i-1] objectAtIndex:0];
y = previousCellRow.frame.origin.y + previousCellRow.frame.size.height;
if (cellOffset.y != 0)
y += cellOffset.y;
}
if (j == 0) {
x = 0.0f;
} else {
DTGridViewCellInfo *previousCellRow = [cellInfoArrayCols objectAtIndex:j-1];
x = previousCellRow.frame.origin.x + previousCellRow.frame.size.width;
if (cellOffset.x != 0)
x += cellOffset.x;
}
if (maxHeight < y + height)
maxHeight = y + height;
if (maxWidth < x + width)
maxWidth = x + width;
info.frame = CGRectMake(x,y,width,height);
[cellInfoArrayCols addObject:info];
[info release];
}
[cellInfoArrayRows addObject:cellInfoArrayCols];
[cellInfoArrayCols release];
}
self.contentSize = CGSizeMake(maxWidth, maxHeight);
// If we're going right to left, we want to shift all the rows to the right
if (self.layoutDirectionality & DTGridViewDirectionalityRightToLeft) {
for (NSArray *cellInfoArrayCols in cellInfoArrayRows) {
DTGridViewCellInfo *rightMostCellInfo = (DTGridViewCellInfo *)[cellInfoArrayCols lastObject];
CGFloat rightMostX = rightMostCellInfo.frame.origin.x + rightMostCellInfo.frame.size.width;
if (rightMostX < maxWidth) {
// Shift every cell over to the right
for (NSUInteger col = 0; col < [cellInfoArrayCols count]; ++col) {
DTGridViewCellInfo *cellInfo = [cellInfoArrayCols objectAtIndex:col];
// Let the first cell fill the gap
if (col == 0) {
CGRect cellInfoFrame = cellInfo.frame;
cellInfoFrame.origin.x = (maxWidth - rightMostX);
cellInfo.frame = cellInfoFrame;
} else {
DTGridViewCellInfo *previousCellRow = [cellInfoArrayCols objectAtIndex:col-1];
CGRect cellInfoFrame = cellInfo.frame;
cellInfoFrame.origin.x = previousCellRow.frame.origin.x + previousCellRow.frame.size.width;
cellInfo.frame = cellInfoFrame;
}
}
}
}
self.contentOffset = CGPointMake(maxWidth - self.frame.size.width, 0.0);
}
self.gridCells = cellInfoArrayRows;
[cellInfoArrayRows release];
if ([self.subviews count] > [self.gridCells count]) {
// the underlying data must have reduced, time to iterate
NSSet *gridCellsSet = [NSSet setWithArray:self.gridCells];
NSArray *subviewsCopy = [self.subviews copy];
for (UIView *cell in subviewsCopy) {
if (
[cell isKindOfClass:[DTGridViewCell class]] &&
![gridCellsSet member:cell]
)
{
[cell removeFromSuperview];
}
}
[subviewsCopy release];
}
}
- (void)checkViews {
if ([cellInfoForCellsOnScreen count] == 0) {
[self initialiseViews];
return;
}
NSMutableDictionary *leftRightCells = [[NSMutableDictionary alloc] init];
NSArray *orderedCells = [cellInfoForCellsOnScreen copy];
BOOL isGoingUp = NO;
BOOL isGoingDown = NO;
BOOL isGoingLeft = NO;
BOOL isGoingRight = NO;
if (self.contentOffset.y < oldContentOffset.y && self.contentOffset.y >= 0)
isGoingUp = YES;
else if (self.contentOffset.y > oldContentOffset.y && self.contentOffset.y + self.frame.size.height < self.contentSize.height)
isGoingDown = YES;
else if (hasResized)
isGoingUp = YES;
if (self.contentOffset.x < oldContentOffset.x && self.contentOffset.x >= 0)
isGoingLeft = YES;
else if (self.contentOffset.x > oldContentOffset.x && self.contentOffset.x + self.frame.size.width < self.contentSize.width)
isGoingRight = YES;
else if (hasResized)
isGoingRight = YES;
// NSLog(@"isGoingUp: %i, isGoingDown: %i, co.y: %f, old.y: %f", isGoingUp, isGoingDown, self.contentOffset.y, oldContentOffset.y);
hasResized = NO;
oldContentOffset = self.contentOffset;
for (DTGridViewCellInfo *info in orderedCells) {
if (isGoingLeft) {
if (info.xPosition > 0 && info.frame.origin.x > self.contentOffset.x) {
if (![leftRightCells objectForKey:[NSString stringWithFormat:@"%i", info.yPosition]])
[leftRightCells setObject:info forKey:[NSString stringWithFormat:@"%i", info.yPosition]];
else if ([[leftRightCells objectForKey:[NSString stringWithFormat:@"%i", info.yPosition]] xPosition] > info.xPosition)
[leftRightCells setObject:info forKey:[NSString stringWithFormat:@"%i", info.yPosition]];
}
} else if (isGoingRight) {
if ([[self.gridCells objectAtIndex:info.yPosition] count] - 1 > info.xPosition && info.frame.origin.x + info.frame.size.width < self.contentOffset.x + self.frame.size.width) {
if (![leftRightCells objectForKey:[NSString stringWithFormat:@"%i", info.yPosition]])
[leftRightCells setObject:info forKey:[NSString stringWithFormat:@"%i", info.yPosition]];
else if ([[leftRightCells objectForKey:[NSString stringWithFormat:@"%i", info.yPosition]] xPosition] < info.xPosition)
[leftRightCells setObject:info forKey:[NSString stringWithFormat:@"%i", info.yPosition]];
}
}
if (![self cellInfoShouldBeOnShow:info])
[self removeCellWithInfo:info];
}
if (isGoingLeft) {
for (NSString *yPos in [leftRightCells allKeys]) {
DTGridViewCellInfo *info = [leftRightCells objectForKey:yPos];
[self checkRow:info.yPosition column:info.xPosition goingLeft:YES];
}
} else if (isGoingRight) {
for (NSString *yPos in [leftRightCells allKeys]) {
DTGridViewCellInfo *info = [leftRightCells objectForKey:yPos];
[self checkRow:info.yPosition column:info.xPosition goingLeft:NO];
}
}
if (isGoingUp)
[self checkNewRowStartingWithCellInfo:[orderedCells objectAtIndex:0] goingUp:YES];
else if (isGoingDown)
[self checkNewRowStartingWithCellInfo:[orderedCells lastObject] goingUp:NO];
[leftRightCells release];
[orderedCells release];
}
- (void)initialiseViews {
for (NSUInteger i = 0; i < [cellInfoForCellsOnScreen count]; i++) {
DTGridViewCellInfo *info = [cellInfoForCellsOnScreen objectAtIndex:i];
if (![self cellInfoShouldBeOnShow:info])
[self removeCellWithInfo:info];
}
for (NSUInteger i = 0; i < [gridCells count]; i++) {
NSMutableArray *row = [gridCells objectAtIndex:i];
for (NSUInteger j = 0; j < [row count]; j++) {
id object = [row objectAtIndex:j];
if ([object isMemberOfClass:[DTGridViewCellInfo class]]) {
DTGridViewCellInfo *info = (DTGridViewCellInfo *)object;
if ([self cellInfoShouldBeOnShow:info])
[self addCellWithInfo:info];
}
}
}
}
- (void)checkRow:(NSInteger)row column:(NSInteger)col goingLeft:(BOOL)goingLeft {
NSObject<DTGridViewCellInfoProtocol> *info = [self cellInfoForRow:row column:col];
if (!info) return;
if ([self cellInfoShouldBeOnShow:info])
[self addCellWithInfo:info];
if (goingLeft) {
if (info.frame.origin.x > self.contentOffset.x)
[self checkRow:row column:(col - 1) goingLeft:goingLeft];
} else {
if (info.frame.origin.x + info.frame.size.width < self.contentOffset.x + self.frame.size.width)
[self checkRow:row column:(col + 1) goingLeft:goingLeft];
}
}
- (NSObject<DTGridViewCellInfoProtocol> *)cellInfoForRow:(NSUInteger)row column:(NSUInteger)col {
if ([self.gridCells count] <= row) return nil;
NSArray *rowArray = [self.gridCells objectAtIndex:row];
if ([rowArray count] <= col) return nil;
return (NSObject<DTGridViewCellInfoProtocol> *)[rowArray objectAtIndex:col];
}
- (void)checkNewRowStartingWithCellInfo:(NSObject<DTGridViewCellInfoProtocol> *)info goingUp:(BOOL)goingUp {
//NSLog(@"%@", info);
if (!info) return;
if (![self rowOfCellInfoShouldBeOnShow:info]) return;
NSObject<DTGridViewCellInfoProtocol> *infoToCheck = info;
NSInteger row = info.yPosition;
NSInteger total = [[self.gridCells objectAtIndex:row] count];
NSInteger goingRightPosition = info.xPosition;
NSInteger goingLeftPosition = info.xPosition;
BOOL goingLeft = NO;
while (![self cellInfoShouldBeOnShow:infoToCheck]) {
goingLeft = !goingLeft;
if (goingLeft)
infoToCheck = [self cellInfoForRow:row column:--goingLeftPosition];
else
infoToCheck = [self cellInfoForRow:row column:++goingRightPosition];
if (goingRightPosition > total)
return;
}
if ([infoToCheck isEqual:info]) {
[self checkRow:infoToCheck.yPosition column:infoToCheck.xPosition goingLeft:YES];
[self checkRow:infoToCheck.yPosition column:infoToCheck.xPosition goingLeft:NO];
} else {
[self checkRow:infoToCheck.yPosition column:infoToCheck.xPosition goingLeft:goingLeft];
}
NSObject<DTGridViewCellInfoProtocol> *nextInfo = nil;
if (goingUp)
nextInfo = [self cellInfoForRow:info.yPosition - 1 column:info.xPosition];
else
nextInfo = [self cellInfoForRow:info.yPosition + 1 column:info.xPosition];
if (nextInfo)
[self checkNewRowStartingWithCellInfo:nextInfo goingUp:goingUp];
}
#pragma mark Public methods
- (DTGridViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier {
for (DTGridViewCell *c in freeCells) {
if ([c.identifier isEqualToString:identifier]) {
[c retain];
[freeCells removeObject:c];
[c prepareForReuse];
return [c autorelease];
}
}
return nil;
}
- (DTGridViewCell *)cellForRow:(NSUInteger)rowIndex column:(NSUInteger)columnIndex {
for (UIView *v in self.subviews) {
if ([v isKindOfClass:[DTGridViewCell class]]) {
DTGridViewCell *c = (DTGridViewCell *)v;
if (c.xPosition == columnIndex && c.yPosition == rowIndex)
return c;
}
}
return nil;
}
- (void)scrollViewToRow:(NSUInteger)rowIndex column:(NSUInteger)columnIndex scrollPosition:(DTGridViewScrollPosition)position animated:(BOOL)animated {
CGFloat xPos = 0, yPos = 0;
CGRect cellFrame = [[[self.gridCells objectAtIndex:rowIndex] objectAtIndex:columnIndex] frame];
// working out x co-ord
if (position == DTGridViewScrollPositionTopLeft || position == DTGridViewScrollPositionMiddleLeft || position == DTGridViewScrollPositionBottomLeft)
xPos = cellFrame.origin.x;
else if (position == DTGridViewScrollPositionTopRight || position == DTGridViewScrollPositionMiddleRight || position == DTGridViewScrollPositionBottomRight)
xPos = cellFrame.origin.x + cellFrame.size.width - self.frame.size.width;
else if (position == DTGridViewScrollPositionTopCenter || position == DTGridViewScrollPositionMiddleCenter || position == DTGridViewScrollPositionBottomCenter)
xPos = (cellFrame.origin.x + (cellFrame.size.width / 2)) - (self.frame.size.width / 2);
else if (position == DTGridViewScrollPositionNone) {
BOOL isBig = NO;
if (cellFrame.size.width > self.frame.size.width)
isBig = YES;
if ((cellFrame.origin.x < self.contentOffset.x)
&& ((cellFrame.origin.x + cellFrame.size.width) > (self.contentOffset.x + self.frame.size.width)))
xPos = self.contentOffset.x;
else if (cellFrame.origin.x < self.contentOffset.x)
if (isBig)
xPos = (cellFrame.origin.x + cellFrame.size.width) - self.frame.size.width;
else
xPos = cellFrame.origin.x;
else if ((cellFrame.origin.x + cellFrame.size.width) > (self.contentOffset.x + self.frame.size.width))
if (isBig)
xPos = cellFrame.origin.x;
else
xPos = (cellFrame.origin.x + cellFrame.size.width) - self.frame.size.width;
else
xPos = self.contentOffset.x;
}
// working out y co-ord
if (position == DTGridViewScrollPositionTopLeft || position == DTGridViewScrollPositionTopCenter || position == DTGridViewScrollPositionTopRight) {
yPos = cellFrame.origin.y;
} else if (position == DTGridViewScrollPositionBottomLeft || position == DTGridViewScrollPositionBottomCenter || position == DTGridViewScrollPositionBottomRight) {
yPos = cellFrame.origin.y + cellFrame.size.height - self.frame.size.height;
} else if (position == DTGridViewScrollPositionMiddleLeft || position == DTGridViewScrollPositionMiddleCenter || position == DTGridViewScrollPositionMiddleRight) {
yPos = (cellFrame.origin.y + (cellFrame.size.height / 2)) - (self.frame.size.height / 2);
} else if (position == DTGridViewScrollPositionNone) {
BOOL isBig = NO;
if (cellFrame.size.height > self.frame.size.height)
isBig = YES;
if ((cellFrame.origin.y < self.contentOffset.y)
&& ((cellFrame.origin.y + cellFrame.size.height) > (self.contentOffset.y + self.frame.size.height)))
yPos = self.contentOffset.y;
else if (cellFrame.origin.y < self.contentOffset.y)
if (isBig)
yPos = (cellFrame.origin.y + cellFrame.size.height) - self.frame.size.height;
else
yPos = cellFrame.origin.y;
else if ((cellFrame.origin.y + cellFrame.size.height) > (self.contentOffset.y + self.frame.size.height))
if (isBig)
yPos = cellFrame.origin.y;
else
yPos = (cellFrame.origin.y + cellFrame.size.height) - self.frame.size.height;
else
yPos = self.contentOffset.y;
}
if (xPos == self.contentOffset.x && yPos == self.contentOffset.y)
return;
if (xPos > self.contentSize.width - self.frame.size.width)
xPos = self.contentSize.width - self.frame.size.width;
else if (xPos < 0)
xPos = 0.0f;
if (yPos > self.contentSize.height - self.frame.size.height)
yPos = self.contentSize.height - self.frame.size.height;
else if (yPos < 0)
yPos = 0.0f;
[self scrollRectToVisible:CGRectMake(xPos, yPos, self.frame.size.width, self.frame.size.height) animated:animated];
if (!animated)
[self checkViews];
if ([self.delegate respondsToSelector:@selector(gridView:didProgrammaticallyScrollToRow:column:)])
[self.delegate gridView:self didProgrammaticallyScrollToRow:rowIndex column:columnIndex];
}
- (void)selectRow:(NSUInteger)rowIndex column:(NSUInteger)columnIndex scrollPosition:(DTGridViewScrollPosition)position animated:(BOOL)animated {
for (UIView *v in self.subviews) {
if ([v isKindOfClass:[DTGridViewCell class]]) {
DTGridViewCell *c = (DTGridViewCell *)v;
if (c.xPosition == columnIndex && c.yPosition == rowIndex)
c.selected = YES;
else if (c.xPosition == columnIndexOfSelectedCell && c.yPosition == rowIndexOfSelectedCell)
c.selected = NO;
}
}
rowIndexOfSelectedCell = rowIndex;
columnIndexOfSelectedCell = columnIndex;
[self scrollViewToRow:rowIndex column:columnIndex scrollPosition:position animated:animated];
}
- (void)fireEdgeScroll {
if (self.pagingEnabled)
if ([self.delegate respondsToSelector:@selector(pagedGridView:didScrollToRow:column:)])
[self.delegate pagedGridView:self didScrollToRow:((NSInteger)(self.contentOffset.y / self.frame.size.height)) column:((NSInteger)(self.contentOffset.x / self.frame.size.width))];
if ([self.delegate respondsToSelector:@selector(gridView:scrolledToEdge:)]) {
if (self.contentOffset.x <= 0)
[self.delegate gridView:self scrolledToEdge:DTGridViewEdgeLeft];
if (self.contentOffset.x >= self.contentSize.width - self.frame.size.width)
[self.delegate gridView:self scrolledToEdge:DTGridViewEdgeRight];
if (self.contentOffset.y <= 0)
[self.delegate gridView:self scrolledToEdge:DTGridViewEdgeTop];
if (self.contentOffset.y >= self.contentSize.height - self.frame.size.height)
[self.delegate gridView:self scrolledToEdge:DTGridViewEdgeBottom];
}
}
- (void)gridViewCellWasTouched:(DTGridViewCell *)cell {
[self bringSubviewToFront:cell];
if ([self.delegate respondsToSelector:@selector(gridView:selectionMadeAtRow:column:)]) {
// Reverse the columns if we're going right to left.
NSUInteger columnIndex = cell.xPosition;
if (self.layoutDirectionality & DTGridViewDirectionalityRightToLeft) {
columnIndex = [self.dataSource numberOfColumnsInGridView:self forRowWithIndex:rowIndexOfSelectedCell] - columnIndex - 1;
}
[self.delegate gridView:self selectionMadeAtRow:cell.yPosition column:columnIndex];
}
}
#pragma mark -
#pragma mark Accessors
- (NSInteger)numberOfRows {
if (numberOfRows == DTGridViewInvalid) {
numberOfRows = [self.dataSource numberOfRowsInGridView:self];
}
return numberOfRows;
}
@end