Skip to content
This repository

Issue #96:Fix for Buggy horizontal mode #103

Open
wants to merge 1 commit into from

4 participants

RadhikaS Mark Danks Gabriel Rinaldi Evadne Wu
RadhikaS

Added changes to support horizontal layout.

Mark Danks

I pulled this into my fork and it worked great.

Gabriel Rinaldi

This fix works great, why don't you accept this?

Evadne Wu
Collaborator
Evadne Wu
Collaborator

Thanks for the contribution. After re-reviewing the patch, I regret to say that it might not be pulled into the next master release in its current form due to quality issues. It still contains a lot of useful ideas and I understand that vertical support is an important aspect of the project. It’ll be given priority treatment.

Thanks!

Gabriel Rinaldi

The problem for me is horizontal scrolling. It does not work, it is broken. I did not make this changes, but I know they work :) (but not if they break something else).

Evadne Wu
Collaborator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Dec 15, 2011
RadhikaS Issue #96:Fix for Buggy horizontal mode f57da78
This page is out of date. Refresh to see the latest.
7  Classes/AQGridView.h
@@ -130,6 +130,7 @@ extern NSString * const AQGridViewSelectionDidChangeNotification;
130 130
 	UIView *						_headerView;
131 131
 	UIView *						_footerView;
132 132
 
  133
+
133 134
 	struct
134 135
 	{
135 136
 		unsigned	resizesCellWidths:1;
@@ -138,7 +139,10 @@ extern NSString * const AQGridViewSelectionDidChangeNotification;
138 139
 		unsigned	allowsSelection:1;
139 140
 		unsigned	backgroundViewExtendsUp:1;
140 141
 		unsigned	backgroundViewExtendsDown:1;
  142
+        unsigned	backgroundViewExtendsLeft:1;
  143
+		unsigned	backgroundViewExtendsRight:1;
141 144
 		unsigned	usesPagedHorizontalScrolling:1;
  145
+        unsigned    layoutChange:1;
142 146
 		unsigned	updating:1;				// unused
143 147
 		unsigned	ignoreTouchSelect:1;
144 148
 		unsigned	needsReload:1;
@@ -230,7 +234,10 @@ extern NSString * const AQGridViewSelectionDidChangeNotification;
230 234
 @property (nonatomic, retain) UIView * backgroundView;		// specifies a view to place behind the cells
231 235
 @property (nonatomic) BOOL backgroundViewExtendsUp;			// default is NO. If YES, the background view extends upward and is visible during a bounce.
232 236
 @property (nonatomic) BOOL backgroundViewExtendsDown;		// default is NO. If YES, the background view extends downward and is visible during a bounce.
  237
+@property (nonatomic) BOOL backgroundViewExtendsLeft;			// default is NO. If YES, the background view extends left and is visible during a bounce.
  238
+@property (nonatomic) BOOL backgroundViewExtendsRight;		// default is NO. If YES, the background view extends right and is visible during a bounce.
233 239
 @property (nonatomic) BOOL usesPagedHorizontalScrolling;	// default is NO, and scrolls verticalls only. Set to YES to have horizontal-only scrolling by page.
  240
+@property (nonatomic) BOOL layoutChange;			
234 241
 
235 242
 @property (nonatomic) AQGridViewCellSeparatorStyle separatorStyle;	// default is AQGridViewCellSeparatorStyleEmptySpace
236 243
 @property (nonatomic, retain) UIColor * separatorColor;		// ignored unless separatorStyle == AQGridViewCellSeparatorStyleSingleLine. Default is standard separator gray.
168  Classes/AQGridView.m
@@ -112,6 +112,7 @@ - (void) _sharedGridViewInit
112 112
     self.canCancelContentTouches = YES;
113 113
 
114 114
 	_selectedIndex = NSNotFound;
  115
+    _flags.layoutChange = NO;
115 116
 	_pendingSelectionIndex = NSNotFound;
116 117
 
117 118
 	_flags.resizesCellWidths = 0;
@@ -120,6 +121,7 @@ - (void) _sharedGridViewInit
120 121
 	_flags.allowsSelection = 1;
121 122
 	_flags.usesPagedHorizontalScrolling = NO;
122 123
 	_flags.contentSizeFillsBounds = 1;
  124
+    
123 125
 }
124 126
 
125 127
 - (id)initWithFrame: (CGRect) frame
@@ -200,6 +202,8 @@ - (AQGridViewLayoutDirection) layoutDirection
200 202
 - (void) setLayoutDirection: (AQGridViewLayoutDirection) direction
201 203
 {
202 204
 	_gridData.layoutDirection = direction;
  205
+    _flags.layoutChange = YES;
  206
+
203 207
 }
204 208
 
205 209
 - (NSUInteger) numberOfItems
@@ -249,6 +253,27 @@ - (void) setBackgroundViewExtendsUp: (BOOL) value
249 253
 	_flags.backgroundViewExtendsUp = (value ? 1 : 0);
250 254
 }
251 255
 
  256
+- (BOOL) backgroundViewExtendsLeft
  257
+{
  258
+	return ( _flags.backgroundViewExtendsLeft);
  259
+}
  260
+
  261
+- (void) setBackgroundViewExtendsLeft: (BOOL) value
  262
+{
  263
+	_flags.backgroundViewExtendsLeft = (value ? 1 : 0);
  264
+}
  265
+
  266
+
  267
+- (BOOL) backgroundViewExtendsRight
  268
+{
  269
+	return ( _flags.backgroundViewExtendsRight);
  270
+}
  271
+
  272
+- (void) setBackgroundViewExtendsRight: (BOOL) value
  273
+{
  274
+	_flags.backgroundViewExtendsRight = (value ? 1 : 0);
  275
+}
  276
+
252 277
 - (BOOL) requiresSelection
253 278
 {
254 279
 	return ( _flags.requiresSelection );
@@ -259,6 +284,16 @@ - (void) setRequiresSelection: (BOOL) value
259 284
 	_flags.requiresSelection = (value ? 1 : 0);
260 285
 }
261 286
 
  287
+- (BOOL) layoutChange
  288
+{
  289
+	return ( _flags.layoutChange );
  290
+}
  291
+
  292
+- (void) setLayoutChange: (BOOL) value
  293
+{
  294
+	_flags.layoutChange = (value ? 1 : 0);
  295
+}
  296
+
262 297
 - (BOOL) resizesCellWidthToFit
263 298
 {
264 299
 	return ( _flags.resizesCellWidths );
@@ -510,6 +545,8 @@ - (void) setContentSize: (CGSize) newSize
510 545
 {
511 546
 	if ( (_flags.contentSizeFillsBounds == 1) && (newSize.height < self.bounds.size.height) )
512 547
 		newSize.height = self.bounds.size.height;
  548
+    if ( (_flags.contentSizeFillsBounds == 1) && (newSize.width < self.bounds.size.width) )
  549
+		newSize.width = self.bounds.size.width;
513 550
 
514 551
 	if (self.gridFooterView)
515 552
 	{
@@ -522,9 +559,12 @@ - (void) setContentSize: (CGSize) newSize
522 559
 	    if (newSize.height < footerHeight + minimumHeight)
523 560
 	        newSize.height = minimumHeight;
524 561
 	}
525  
-
526  
-	newSize.height = fmax(newSize.height, self.frame.size.height+1);
527  
-
  562
+    if(self.layoutDirection == AQGridViewLayoutDirectionVertical){
  563
+        newSize.height = fmax(newSize.height, self.frame.size.height+1);
  564
+    }else{
  565
+        newSize.width = fmax(newSize.width, self.frame.size.width+1);
  566
+        
  567
+    }
528 568
 	CGSize oldSize = self.contentSize;
529 569
 	[super setContentSize: newSize];
530 570
 
@@ -558,6 +598,10 @@ - (void) setBounds: (CGRect) bounds
558 598
 
559 599
 	if ( !CGSizeEqualToSize(bounds.size, oldBounds.size) )
560 600
 		[self handleGridViewBoundsChanged: oldBounds toNewBounds: bounds];
  601
+    if ( _flags.layoutChange == YES ){
  602
+		[self handleGridViewBoundsChanged: oldBounds toNewBounds: bounds];
  603
+        _flags.layoutChange = NO;
  604
+    }
561 605
 }
562 606
 
563 607
 - (BOOL) isEditing
@@ -667,40 +711,77 @@ - (void) layoutSubviews
667 711
 	}
668 712
 
669 713
 	CGRect rect = CGRectZero;
670  
-	rect.size.width = self.bounds.size.width;
671  
-	rect.size.height = self.contentSize.height -  (_gridData.topPadding + _gridData.bottomPadding);
672  
-	rect.origin.y += _gridData.topPadding;
673  
-
  714
+    
  715
+    if(self.layoutDirection == AQGridViewLayoutDirectionVertical){
  716
+        rect.size.width = self.bounds.size.width;
  717
+        rect.size.height = self.contentSize.height -  (_gridData.topPadding + _gridData.bottomPadding);
  718
+        rect.origin.y += _gridData.topPadding;
  719
+    }else{
  720
+        rect.size.height = self.bounds.size.height;
  721
+        rect.size.width = self.contentSize.width -  (_gridData.leftPadding + _gridData.rightPadding);
  722
+        rect.origin.x += _gridData.leftPadding;
  723
+    }
674 724
 	// Make sure background is an integral number of rows tall. That way, it draws patterned colours correctly on all OSes.
675 725
 	CGRect backgroundRect = rect;
676  
-
677  
-	if ([self backgroundViewExtendsUp]) {
678  
-		backgroundRect.origin.y = backgroundRect.origin.y - MAX_BOUNCE_DISTANCE;
679  
-		backgroundRect.size.height += MAX_BOUNCE_DISTANCE;	// don't just move it, grow it
680  
-	}
681  
-
682  
-	if ([self backgroundViewExtendsDown]) {
683  
-		backgroundRect.size.height = backgroundRect.size.height + MAX_BOUNCE_DISTANCE;
684  
-	}
685  
-
  726
+    
  727
+       
686 728
 	CGFloat minimumHeight = rect.size.height,
687  
-		actualHeight = 0;
  729
+    minimumWidth = rect.size.width,
  730
+    actualWidth = 0,
  731
+    actualHeight = 0;
  732
+    if(self.layoutDirection == AQGridViewLayoutDirectionVertical){
  733
+        if (([_gridData numberOfItems] == 0) || ([_gridData numberOfItemsPerRow] == 0)) {
688 734
 
689  
-	if (([_gridData numberOfItems] == 0) || ([_gridData numberOfItemsPerRow] == 0)) {
  735
+            actualHeight = [_gridData cellSize].height;
690 736
 
691  
-		actualHeight = [_gridData cellSize].height;
  737
+        } else {
692 738
 
693  
-	} else {
  739
+            actualHeight = [_gridData cellSize].height * (ceilf((CGFloat)[_gridData numberOfItems] / (CGFloat)[_gridData numberOfItemsPerRow]) + 1);
694 740
 
695  
-		actualHeight = [_gridData cellSize].height * ([_gridData numberOfItems] / [_gridData numberOfItemsPerRow] + 1);
  741
+        }
  742
+        for (; actualHeight < minimumHeight; actualHeight += [_gridData cellSize].height) {
  743
+        }
  744
+        backgroundRect.size.height = actualHeight;
  745
+    }else{
  746
+        if (([_gridData numberOfItems] == 0) || ([_gridData numberOfItemsPerColumn] == 0)) {
  747
+            
  748
+            actualWidth = [_gridData cellSize].width;
  749
+            
  750
+        } else {
  751
+            
  752
+            actualWidth = [_gridData cellSize].width * (ceilf((CGFloat)[_gridData numberOfItems] / (CGFloat)[_gridData numberOfItemsPerColumn]) + 1);
696 753
 
697  
-	}
698  
-	for (; actualHeight < minimumHeight; actualHeight += [_gridData cellSize].height) {
699  
-	}
700  
-	backgroundRect.size.height = actualHeight;
  754
+        }
  755
+        for (; actualWidth < minimumWidth; actualWidth += [_gridData cellSize].width) {
  756
+        }
  757
+        backgroundRect.size.width = actualWidth;
  758
+        
  759
+    }
701 760
 
  761
+    if(self.layoutDirection == AQGridViewLayoutDirectionVertical){
  762
+        
  763
+        if ([self backgroundViewExtendsUp]) {
  764
+            backgroundRect.origin.y = backgroundRect.origin.y - MAX_BOUNCE_DISTANCE;
  765
+            backgroundRect.size.height += MAX_BOUNCE_DISTANCE;	// don't just move it, grow it
  766
+            
  767
+        }
  768
+        
  769
+        if ([self backgroundViewExtendsDown]) {
  770
+            backgroundRect.size.height = backgroundRect.size.height + MAX_BOUNCE_DISTANCE;
  771
+        }
  772
+    }
  773
+    else{
  774
+        if ([self backgroundViewExtendsLeft]) {
  775
+            backgroundRect.origin.x = backgroundRect.origin.x - MAX_BOUNCE_DISTANCE;
  776
+            backgroundRect.size.width += MAX_BOUNCE_DISTANCE;	// don't just move it, grow it
  777
+        }
  778
+        
  779
+        if ([self backgroundViewExtendsRight]) {
  780
+            backgroundRect.size.width = backgroundRect.size.width + MAX_BOUNCE_DISTANCE;
  781
+        }
  782
+    }
702 783
 
703  
-	self.backgroundView.frame = backgroundRect;
  784
+    self.backgroundView.frame = backgroundRect;
704 785
 
705 786
 	if ( _headerView != nil )
706 787
 	{
@@ -719,6 +800,8 @@ - (void) layoutSubviews
719 800
 		_footerView.frame = rect;
720 801
 		[self bringSubviewToFront:_footerView];
721 802
 	}
  803
+    
  804
+    
722 805
 }
723 806
 
724 807
 - (CGRect) rectForItemAtIndex: (NSUInteger) index
@@ -874,7 +957,12 @@ - (void) fixCellsFromAnimation
874 957
 
875 958
 	//NSAssert([newVisibleCells count] == _visibleIndices.length, @"visible cell count after animation doesn't match visible indices");
876 959
 
877  
-	[newVisibleCells sortUsingSelector: @selector(compareOriginAgainstCell:)];
  960
+    if(self.layoutDirection == AQGridViewLayoutDirectionVertical){
  961
+        [newVisibleCells sortUsingSelector: @selector(compareOriginAgainstCellVertical:)];
  962
+    }else
  963
+    {
  964
+        [newVisibleCells sortUsingSelector: @selector(compareOriginAgainstCellHorizontal:)];
  965
+    }
878 966
 	[_visibleCells removeObjectsInArray: newVisibleCells];
879 967
 	[_visibleCells makeObjectsPerformSelector: @selector(removeFromSuperview)];
880 968
 	[_visibleCells setArray: newVisibleCells];
@@ -1190,14 +1278,26 @@ - (void) setBackgroundView: (UIView *) newView
1190 1278
 
1191 1279
 	CGRect backgroundRect = CGRectMake(0.0f, 0.0f, self.bounds.size.width, self.bounds.size.height);
1192 1280
 
1193  
-	if ([self backgroundViewExtendsUp]) {
1194  
-		backgroundRect.origin.y = backgroundRect.origin.y - MAX_BOUNCE_DISTANCE;
1195  
-		backgroundRect.size.height += MAX_BOUNCE_DISTANCE;		// don't just move it, grow it
1196  
-	}
  1281
+    if(self.layoutDirection == AQGridViewLayoutDirectionVertical){
  1282
+        if ([self backgroundViewExtendsUp]) {
  1283
+            backgroundRect.origin.y = backgroundRect.origin.y - MAX_BOUNCE_DISTANCE;
  1284
+            backgroundRect.size.height += MAX_BOUNCE_DISTANCE;		// don't just move it, grow it
  1285
+        }
1197 1286
 
1198  
-	if ([self backgroundViewExtendsDown]) {
1199  
-		backgroundRect.size.height = backgroundRect.size.height + MAX_BOUNCE_DISTANCE;
  1287
+        if ([self backgroundViewExtendsDown]) {
  1288
+            backgroundRect.size.height = backgroundRect.size.height + MAX_BOUNCE_DISTANCE;
  1289
+        }
  1290
+    }else{
  1291
+    
  1292
+    if ([self backgroundViewExtendsLeft]) {
  1293
+		backgroundRect.origin.x = backgroundRect.origin.x - MAX_BOUNCE_DISTANCE;
  1294
+		backgroundRect.size.width += MAX_BOUNCE_DISTANCE;		// don't just move it, grow it
1200 1295
 	}
  1296
+    
  1297
+	if ([self backgroundViewExtendsRight]) {
  1298
+		backgroundRect.size.width = backgroundRect.size.width + MAX_BOUNCE_DISTANCE;
  1299
+	}
  1300
+    }
1201 1301
 
1202 1302
 	_backgroundView.frame = backgroundRect;
1203 1303
 
4  Classes/AQGridViewCell.h
@@ -122,6 +122,6 @@ typedef enum {
122 122
 - (void)setEditing:(BOOL)editing animated:(BOOL)animated;
123 123
 
124 124
 // Sorting
125  
-- (NSComparisonResult) compareOriginAgainstCell: (AQGridViewCell *) otherCell;
126  
-
  125
+- (NSComparisonResult) compareOriginAgainstCellVertical: (AQGridViewCell *) otherCell;
  126
+- (NSComparisonResult) compareOriginAgainstCellHorizontal: (AQGridViewCell *) otherCell;
127 127
 @end
22  Classes/AQGridViewCell.m
@@ -100,7 +100,8 @@ - (void) dealloc
100 100
 		CFRelease( _selectionColorInfo );
101 101
 }
102 102
 
103  
-- (NSComparisonResult) compareOriginAgainstCell: (AQGridViewCell *) otherCell
  103
+
  104
+- (NSComparisonResult) compareOriginAgainstCellVertical: (AQGridViewCell *) otherCell
104 105
 {
105 106
 	CGPoint myOrigin = self.frame.origin;
106 107
 	CGPoint theirOrigin = otherCell.frame.origin;
@@ -118,6 +119,25 @@ - (NSComparisonResult) compareOriginAgainstCell: (AQGridViewCell *) otherCell
118 119
 	return ( NSOrderedSame );
119 120
 }
120 121
 
  122
+- (NSComparisonResult) compareOriginAgainstCellHorizontal: (AQGridViewCell *) otherCell
  123
+{
  124
+	CGPoint myOrigin = self.frame.origin;
  125
+	CGPoint theirOrigin = otherCell.frame.origin;
  126
+	
  127
+	if ( myOrigin.x > theirOrigin.x )
  128
+		return ( NSOrderedDescending );
  129
+	else if ( myOrigin.x < theirOrigin.x )
  130
+		return ( NSOrderedAscending );
  131
+    
  132
+    if ( myOrigin.y > theirOrigin.y )
  133
+		return ( NSOrderedDescending );
  134
+	else if ( myOrigin.y < theirOrigin.y )
  135
+		return ( NSOrderedAscending );
  136
+	
  137
+	return ( NSOrderedSame );
  138
+}
  139
+
  140
+
121 141
 - (UIView *) contentView
122 142
 {
123 143
 	if ( _contentView == nil )
2  Classes/AQGridViewData.h
@@ -80,6 +80,8 @@
80 80
 - (CGRect) rectForEntireGrid;
81 81
 - (CGSize) sizeForEntireGrid;
82 82
 - (NSUInteger) numberOfItemsPerRow;
  83
+- (NSUInteger) numberOfItemsPerColumn;
  84
+
83 85
 
84 86
 - (CGRect) cellRectAtIndex: (NSUInteger) index;
85 87
 - (CGRect) cellRectForPoint: (CGPoint) point;
181  Classes/AQGridViewData.m
@@ -39,6 +39,7 @@
39 39
 
40 40
 @interface AQGridViewData (AQGridViewDataPrivate)
41 41
 - (void) fixDesiredCellSizeForWidth: (CGFloat) width;
  42
+- (void) fixDesiredCellSizeForHeight: (CGFloat) height;
42 43
 @end
43 44
 
44 45
 @implementation AQGridViewData
@@ -82,6 +83,8 @@ - (void) gridViewDidChangeBoundsSize: (CGSize) boundsSize
82 83
 	_boundsSize = boundsSize;
83 84
 	if ( _layoutDirection == AQGridViewLayoutDirectionVertical )
84 85
 		[self fixDesiredCellSizeForWidth: boundsSize.width];
  86
+    else
  87
+        [self fixDesiredCellSizeForHeight:boundsSize.height];
85 88
 }
86 89
 
87 90
 - (NSUInteger) itemIndexForPoint: (CGPoint) point
@@ -98,7 +101,13 @@ - (NSUInteger) itemIndexForPoint: (CGPoint) point
98 101
 	NSUInteger x = (NSUInteger)floorf(point.x);
99 102
 	NSUInteger col = x / (NSUInteger)_actualCellSize.width;
100 103
 	
101  
-	NSUInteger result = (row * [self numberOfItemsPerRow]) + col;
  104
+    NSUInteger result;
  105
+    if (_layoutDirection == AQGridViewLayoutDirectionVertical) {
  106
+        result = (row * [self numberOfItemsPerRow]) + col;
  107
+    }else{
  108
+        result = (col * [self numberOfItemsPerColumn]) + row;
  109
+    }
  110
+    
102 111
 	if ( result >= self.numberOfItems )
103 112
 		result = NSNotFound;
104 113
 	
@@ -126,7 +135,7 @@ - (void) setDesiredCellSize: (CGSize) desiredCellSize
126 135
 	if ( _layoutDirection == AQGridViewLayoutDirectionVertical )
127 136
 		[self fixDesiredCellSizeForWidth: _boundsSize.width];
128 137
 	else
129  
-		_actualCellSize = _desiredCellSize;
  138
+        [self fixDesiredCellSizeForHeight:_boundsSize.height];
130 139
 }
131 140
 
132 141
 - (void) setLayoutDirection: (AQGridViewLayoutDirection) direction
@@ -134,7 +143,7 @@ - (void) setLayoutDirection: (AQGridViewLayoutDirection) direction
134 143
 	if ( direction == AQGridViewLayoutDirectionVertical )
135 144
 		[self fixDesiredCellSizeForWidth: _boundsSize.width];
136 145
 	else
137  
-		_actualCellSize = _desiredCellSize;
  146
+        [self fixDesiredCellSizeForHeight:_boundsSize.height];
138 147
 	_layoutDirection = direction;
139 148
 }
140 149
 
@@ -154,18 +163,35 @@ - (CGRect) rectForEntireGrid
154 163
 
155 164
 - (CGSize) sizeForEntireGrid
156 165
 {
157  
-	NSUInteger numPerRow = [self numberOfItemsPerRow];
158  
-    if ( numPerRow == 0 )       // avoid a divide-by-zero exception
159  
-        return ( CGSizeZero );
160  
-	NSUInteger numRows = _numberOfItems / numPerRow;
161  
-	if ( _numberOfItems % numPerRow != 0 )
162  
-		numRows++;
163  
-	
164  
-	CGFloat height = ( ((CGFloat)ceilf((CGFloat)numRows * _actualCellSize.height)) + _topPadding + _bottomPadding );
165  
-	if (height < _gridView.bounds.size.height)
166  
-		height = _gridView.bounds.size.height;
  166
+    if ( _layoutDirection == AQGridViewLayoutDirectionVertical ){
  167
+
  168
+        NSUInteger numPerRow = [self numberOfItemsPerRow];
  169
+        if ( numPerRow == 0 )       // avoid a divide-by-zero exception
  170
+            return ( CGSizeZero );
  171
+        NSUInteger numRows = _numberOfItems / numPerRow;
  172
+        if ( _numberOfItems % numPerRow != 0 )
  173
+            numRows++;
167 174
 	
168  
-	return ( CGSizeMake(((CGFloat)ceilf(_actualCellSize.width * numPerRow)) + _leftPadding + _rightPadding, height) );
  175
+        CGFloat height = ( ((CGFloat)ceilf((CGFloat)numRows * _actualCellSize.height)) + _topPadding + _bottomPadding );
  176
+        if (height < _gridView.bounds.size.height)
  177
+            height = _gridView.bounds.size.height;
  178
+        
  179
+        return ( CGSizeMake(((CGFloat)ceilf(_actualCellSize.width * numPerRow)) + _leftPadding + _rightPadding, height) );
  180
+    }else{
  181
+        NSUInteger numPerCol = [self numberOfItemsPerColumn];
  182
+        if ( numPerCol == 0 )       // avoid a divide-by-zero exception
  183
+            return ( CGSizeZero );
  184
+        NSUInteger numCols = _numberOfItems / numPerCol;
  185
+        if ( _numberOfItems % numPerCol != 0 )
  186
+            numCols++;
  187
+        
  188
+        CGFloat width = ( ((CGFloat)ceilf((CGFloat)numCols * _actualCellSize.width)) + _leftPadding + _rightPadding );
  189
+        if (width < _gridView.bounds.size.width)
  190
+            width = _gridView.bounds.size.width;
  191
+        
  192
+        return ( CGSizeMake(width,((CGFloat)ceilf(_actualCellSize.height * numPerCol)) + _topPadding + _bottomPadding) );
  193
+    }
  194
+
169 195
 }
170 196
 
171 197
 - (NSUInteger) numberOfItemsPerRow
@@ -185,49 +211,99 @@ - (NSUInteger) numberOfItemsPerRow
185 211
 	return ( cols );	
186 212
 }
187 213
 
  214
+- (NSUInteger) numberOfItemsPerColumn
  215
+{
  216
+    NSUInteger cols = (NSUInteger)floorf(_boundsSize.height / _actualCellSize.height);
  217
+	return ( cols );	
  218
+}
  219
+
  220
+
188 221
 - (CGRect) cellRectAtIndex: (NSUInteger) index
189 222
 {
190  
-	NSUInteger numPerRow = [self numberOfItemsPerRow];
191  
-    if ( numPerRow == 0 )       // avoid a divide-by-zero exception
192  
-        return ( CGRectZero );
193  
-	NSUInteger skipRows = index / numPerRow;
194  
-	NSUInteger skipCols = index % numPerRow;
  223
+    CGRect result = CGRectZero;
  224
+    if ( _layoutDirection == AQGridViewLayoutDirectionVertical){
  225
+
  226
+        NSUInteger numPerRow = [self numberOfItemsPerRow];
  227
+        if ( numPerRow == 0 )       // avoid a divide-by-zero exception
  228
+            return ( CGRectZero );
  229
+        NSUInteger skipRows = index / numPerRow;
  230
+        NSUInteger skipCols = index % numPerRow;
195 231
 	
196  
-	CGRect result = CGRectZero;
197  
-	result.origin.x = _actualCellSize.width * (CGFloat)skipCols + _leftPadding;
198  
-	result.origin.y = (_actualCellSize.height  * (CGFloat)skipRows) + _topPadding;
199  
-	result.size = _actualCellSize;
  232
+        result.origin.x = _actualCellSize.width * (CGFloat)skipCols + _leftPadding;
  233
+        result.origin.y = (_actualCellSize.height  * (CGFloat)skipRows) + _topPadding;
  234
+        result.size = _actualCellSize;
200 235
 	
201  
-	return ( result );
  236
+    }else{
  237
+        
  238
+        NSUInteger numPerCol = [self numberOfItemsPerColumn];
  239
+        if (numPerCol == 0)
  240
+            return (CGRectZero);
  241
+        NSUInteger skipCols = index / numPerCol;
  242
+        NSUInteger skipRows = index % numPerCol;
  243
+        
  244
+        result.origin.x = _actualCellSize.width * (CGFloat)skipCols + _leftPadding;
  245
+        result.origin.y = (_actualCellSize.height  * (CGFloat)skipRows) + _topPadding;
  246
+        result.size = _actualCellSize;
  247
+    }
  248
+    return ( result );
  249
+
202 250
 }
203 251
 
204 252
 - (NSIndexSet *) indicesOfCellsInRect: (CGRect) aRect
205 253
 {
206 254
 	NSMutableIndexSet * result = [NSMutableIndexSet indexSet];
207  
-	NSUInteger numPerRow = [self numberOfItemsPerRow];
  255
+    if ( _layoutDirection == AQGridViewLayoutDirectionVertical){
  256
+
  257
+        NSUInteger numPerRow = [self numberOfItemsPerRow];
208 258
 	
209  
-	for ( NSUInteger i = 0; i < _numberOfItems; i++ )
210  
-	{
211  
-		CGRect cellRect = [self cellRectAtIndex: i];
212  
-		
213  
-		if ( CGRectGetMaxY(cellRect) < CGRectGetMinY(aRect) )
214  
-		{
215  
-			// jump forward to the next row
216  
-			i += (numPerRow - 1);
217  
-			continue;
218  
-		}
  259
+        for ( NSUInteger i = 0; i < _numberOfItems; i++ )
  260
+        {
  261
+            CGRect cellRect = [self cellRectAtIndex: i];
  262
+            
  263
+            if ( CGRectGetMaxY(cellRect) < CGRectGetMinY(aRect) )
  264
+            {
  265
+                // jump forward to the next row
  266
+                i += (numPerRow - 1);
  267
+                continue;
  268
+            }
219 269
 		
220  
-		if ( CGRectIntersectsRect(cellRect, aRect) )
221  
-		{
222  
-			[result addIndex: i];
223  
-			if ( (CGRectGetMaxY(cellRect) > CGRectGetMaxY(aRect)) &&
224  
-				 (CGRectGetMaxX(cellRect) > CGRectGetMaxX(aRect)) )
225  
-			{
226  
-				// passed the bottom-right edge of the given rect
227  
-				break;
228  
-			}
229  
-		}
230  
-	}
  270
+            if ( CGRectIntersectsRect(cellRect, aRect) )
  271
+            {
  272
+                [result addIndex: i];
  273
+                if ( (CGRectGetMaxY(cellRect) > CGRectGetMaxY(aRect)) &&
  274
+                    (CGRectGetMaxX(cellRect) > CGRectGetMaxX(aRect)) )
  275
+                {
  276
+                    // passed the bottom-right edge of the given rect
  277
+                    break;
  278
+                }
  279
+            }
  280
+        }
  281
+    }else{
  282
+        NSUInteger numPerCol = [self numberOfItemsPerColumn];
  283
+        for ( NSUInteger i = 0; i < _numberOfItems; i++ )
  284
+        {
  285
+            CGRect cellRect = [self cellRectAtIndex: i];
  286
+            
  287
+            if ( CGRectGetMaxX(cellRect) < CGRectGetMinX(aRect) )
  288
+            {
  289
+                // jump forward to the next column
  290
+                i += (numPerCol - 1);
  291
+                continue;
  292
+            }              
  293
+            
  294
+            if ( CGRectIntersectsRect(cellRect, aRect) )
  295
+            {
  296
+                [result addIndex: i];
  297
+                if ( (CGRectGetMaxY(cellRect) > CGRectGetMaxY(aRect)) &&
  298
+                    (CGRectGetMaxX(cellRect) > CGRectGetMaxX(aRect)) )
  299
+                {
  300
+                    // passed the bottom-right edge of the given rect
  301
+                    break;
  302
+                }
  303
+            }
  304
+        } 
  305
+    }
  306
+
231 307
 	
232 308
 	return ( result );
233 309
 }
@@ -247,4 +323,17 @@ - (void) fixDesiredCellSizeForWidth: (CGFloat) width
247 323
 	_actualCellSize.height = _desiredCellSize.height;
248 324
 }
249 325
 
  326
+- (void) fixDesiredCellSizeForHeight: (CGFloat) height
  327
+{
  328
+    
  329
+    // Much thanks to Brandon Sneed (@bsneed) for the following new algorithm, reduced to two floating-point divisions -- that's O(1) folks!
  330
+	CGFloat h = floorf(height - _topPadding - _bottomPadding);
  331
+	CGFloat dh = floorf(_desiredCellSize.height);
  332
+    CGFloat multiplier = floorf( h / dh );
  333
+	
  334
+	_actualCellSize.height = floorf( h / multiplier );
  335
+	_actualCellSize.width = _desiredCellSize.width;
  336
+    
  337
+}
  338
+
250 339
 @end
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.