Permalink
Browse files

Use drawRect instead of layoutSubviews to draw the cells. Fixes reall…

…y sloppy performance and memory issues. Should all be good now.
  • Loading branch information...
1 parent 98e8c85 commit 11c23308dd5368d5c819cfdbd06ab0c1cb81a41c @renspr renspr committed Jul 20, 2011
Showing with 52 additions and 49 deletions.
  1. +36 −45 MMGridView/Classes/MMGridView.m
  2. +16 −4 MMGridViewDemo/RootViewController.m
View
81 MMGridView/Classes/MMGridView.m
@@ -74,7 +74,33 @@ - (id)initWithCoder:(NSCoder *)aDecoder
}
-- (void)layoutSubviews
+- (void)createSubviews
+{
+ cellMargin = 3;
+ numberOfRows = 3;
+ numberOfColumns = 2;
+ currentPageIndex = 0;
+
+ self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
+ self.contentMode = UIViewContentModeRedraw;
+ self.backgroundColor = [UIColor clearColor];
+
+ self.scrollView = [[[UIScrollView alloc] initWithFrame:self.bounds] autorelease];
+ self.scrollView.delegate = self;
+ self.scrollView.backgroundColor = [UIColor clearColor];
+ self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
+ self.scrollView.alwaysBounceHorizontal = NO;
+ self.scrollView.alwaysBounceVertical = NO;
+ self.scrollView.showsVerticalScrollIndicator = NO;
+ self.scrollView.showsHorizontalScrollIndicator = NO;
+ self.scrollView.pagingEnabled = YES;
+ [self addSubview:self.scrollView];
+
+ [self reloadData];
+}
+
+
+- (void)drawRect:(CGRect)rect
{
if (self.dataSource && self.numberOfRows > 0 && self.numberOfColumns > 0) {
NSInteger noOfCols = self.numberOfColumns;
@@ -95,50 +121,28 @@ - (void)layoutSubviews
CGSize contentSize = CGSizeMake(self.numberOfPages * gridBounds.size.width, gridBounds.size.height);
[self.scrollView setContentSize:contentSize];
+ for (UIView *v in self.scrollView.subviews) {
+ [v removeFromSuperview];
+ }
+
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];
}
}
}
-- (void)createSubviews
-{
- cellMargin = 3;
- numberOfRows = 3;
- numberOfColumns = 2;
- currentPageIndex = 0;
-
- self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
- self.contentMode = UIViewContentModeRedraw;
- self.backgroundColor = [UIColor clearColor];
-
- self.scrollView = [[[UIScrollView alloc] initWithFrame:self.bounds] autorelease];
- self.scrollView.delegate = self;
- self.scrollView.backgroundColor = [UIColor clearColor];
- self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
- self.scrollView.alwaysBounceHorizontal = NO;
- self.scrollView.alwaysBounceVertical = NO;
- self.scrollView.showsVerticalScrollIndicator = NO;
- self.scrollView.showsHorizontalScrollIndicator = NO;
- self.scrollView.pagingEnabled = YES;
- [self addSubview:self.scrollView];
-
- [self reloadData];
-}
-
-
- (void)setDataSource:(id<MMGridViewDataSource>)aDataSource
{
dataSource = aDataSource;
@@ -177,20 +181,7 @@ - (NSUInteger)numberOfPages
- (void)reloadData
{
- for (UIView *v in self.scrollView.subviews) {
- [v removeFromSuperview];
- }
-
- 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]];
- [self.scrollView addSubview:cell];
- }
- }
-
- [self setNeedsLayout];
+ [self setNeedsDisplay];
}
View
20 MMGridViewDemo/RootViewController.m
@@ -26,6 +26,7 @@
@interface RootViewController()
- (void)reload;
+- (void)setupPageControl;
@end
@implementation RootViewController
@@ -41,6 +42,7 @@ - (void)dealloc
[super dealloc];
}
+
- (void)viewDidUnload {
[gridView release];
gridView = nil;
@@ -49,6 +51,7 @@ - (void)viewDidUnload {
[super viewDidUnload];
}
+
- (void)viewDidLoad
{
// Give us a nice title
@@ -60,8 +63,12 @@ - (void)viewDidLoad
action:@selector(reload)];
self.navigationItem.rightBarButtonItem = reloadButton;
[reloadButton release];
+
+ // setup the page control
+ [self setupPageControl];
}
+
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
@@ -76,21 +83,27 @@ - (void)reload
[gridView reloadData];
}
+
+- (void)setupPageControl
+{
+ pageControl.numberOfPages = gridView.numberOfPages;
+ pageControl.currentPage = gridView.currentPageIndex;
+}
+
// ----------------------------------------------------------------------------------
#pragma - MMGridViewDataSource
- (NSInteger)numberOfCellsInGridView:(MMGridView *)gridView
{
- return 15;
+ return 42;
}
- (MMGridViewCell *)gridView:(MMGridView *)gridView cellAtIndex:(NSUInteger)index
{
MMGridViewDefaultCell *cell = [[[MMGridViewDefaultCell alloc] initWithFrame:CGRectNull] autorelease];
cell.textLabel.text = [NSString stringWithFormat:@"Cell %d", index];
- // cell.backgroundView.backgroundColor = [UIColor redColor];
cell.backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"cell-image.png"]];
return cell;
}
@@ -121,8 +134,7 @@ - (void)gridView:(MMGridView *)gridView didDoubleTapCell:(MMGridViewCell *)cell
- (void)gridView:(MMGridView *)theGridView changedPageToIndex:(NSUInteger)index
{
- pageControl.numberOfPages = theGridView.numberOfPages;
- pageControl.currentPage = index;
+ [self setupPageControl];
}
@end

0 comments on commit 11c2330

Please sign in to comment.