Browse files

refactor: separate layout and reloadData to improve performance on sc…

…rolling
  • Loading branch information...
1 parent 835e499 commit 4ee1f8ebcdab587d132705b703638d2ef5becfe4 @siuying siuying committed Jul 14, 2011
Showing with 37 additions and 31 deletions.
  1. +37 −31 MMGridView/Classes/MMGridView.m
View
68 MMGridView/Classes/MMGridView.m
@@ -76,7 +76,40 @@ - (id)initWithCoder:(NSCoder *)aDecoder
- (void)layoutSubviews
{
- [self reloadData];
+ if (self.dataSource && self.numberOfRows > 0 && self.numberOfColumns > 0) {
+ NSInteger noOfCols = self.numberOfColumns;
+ NSInteger noOfRows = self.numberOfRows;
+ NSUInteger cellsPerPage = self.numberOfColumns * self.numberOfRows;
+
+ BOOL isLandscape = UIInterfaceOrientationIsLandscape([[UIDevice currentDevice] orientation]);
+ if (isLandscape) {
+ // In landscape mode switch rows and columns
+ noOfCols = self.numberOfRows;
+ noOfRows = self.numberOfColumns;
+ }
+
+ CGRect gridBounds = self.scrollView.bounds;
+ CGRect cellBounds = CGRectMake(0, 0, gridBounds.size.width / (float)noOfCols,
+ gridBounds.size.height / (float)noOfRows);
+
+ CGSize contentSize = CGSizeMake(self.numberOfPages * gridBounds.size.width, gridBounds.size.height);
+ [self.scrollView setContentSize:contentSize];
+
+ for (NSInteger i = 0; i < [self.dataSource numberOfCellsInGridView:self]; i++) {
+ MMGridViewCell *cell = [self.dataSource gridView:self cellAtIndex:i];
+
+ NSInteger page = (int)floor((float)i / (float)cellsPerPage);
+ NSInteger row = (int)floor((float)i / (float)noOfCols) - (page * noOfRows);
+
+ CGPoint origin = CGPointMake((page * gridBounds.size.width) + ((i % noOfCols) * cellBounds.size.width),
+ (row * cellBounds.size.height));
+
+ CGRect f = CGRectMake(origin.x, origin.y, cellBounds.size.width, cellBounds.size.height);
+ cell.frame = CGRectInset(f, self.cellMargin, self.cellMargin);
+
+ [self.scrollView addSubview:cell];
+ }
+ }
}
@@ -148,43 +181,16 @@ - (void)reloadData
[v removeFromSuperview];
}
- if (self.dataSource && self.numberOfRows > 0 && self.numberOfColumns > 0) {
- NSInteger noOfCols = self.numberOfColumns;
- NSInteger noOfRows = self.numberOfRows;
- NSUInteger cellsPerPage = self.numberOfColumns * self.numberOfRows;
-
- BOOL isLandscape = UIInterfaceOrientationIsLandscape([[UIDevice currentDevice] orientation]);
- if (isLandscape) {
- // In landscape mode switch rows and columns
- noOfCols = self.numberOfRows;
- noOfRows = self.numberOfColumns;
- }
-
- CGRect gridBounds = self.scrollView.bounds;
- CGRect cellBounds = CGRectMake(0, 0, gridBounds.size.width / (float)noOfCols,
- gridBounds.size.height / (float)noOfRows);
-
- CGSize contentSize = CGSizeMake(self.numberOfPages * gridBounds.size.width, gridBounds.size.height);
- [self.scrollView setContentSize:contentSize];
-
+ if (self.dataSource && self.numberOfRows > 0 && self.numberOfColumns > 0) {
for (NSInteger i = 0; i < [self.dataSource numberOfCellsInGridView:self]; i++) {
MMGridViewCell *cell = [self.dataSource gridView:self cellAtIndex:i];
[cell performSelector:@selector(setGridView:) withObject:self];
[cell performSelector:@selector(setIndex:) withObject:[NSNumber numberWithInt:i]];
-
-
- NSInteger page = (int)floor((float)i / (float)cellsPerPage);
- NSInteger row = (int)floor((float)i / (float)noOfCols) - (page * noOfRows);
-
- CGPoint origin = CGPointMake((page * gridBounds.size.width) + ((i % noOfCols) * cellBounds.size.width),
- (row * cellBounds.size.height));
-
- CGRect f = CGRectMake(origin.x, origin.y, cellBounds.size.width, cellBounds.size.height);
- cell.frame = CGRectInset(f, self.cellMargin, self.cellMargin);
-
[self.scrollView addSubview:cell];
}
}
+
+ [self setNeedsLayout];
}

0 comments on commit 4ee1f8e

Please sign in to comment.