From 11c23308dd5368d5c819cfdbd06ab0c1cb81a41c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rene=CC=81=20Sprotte?= Date: Wed, 20 Jul 2011 10:30:39 +0200 Subject: [PATCH] Use drawRect instead of layoutSubviews to draw the cells. Fixes really sloppy performance and memory issues. Should all be good now. --- MMGridView/Classes/MMGridView.m | 81 +++++++++++++---------------- MMGridViewDemo/RootViewController.m | 20 +++++-- 2 files changed, 52 insertions(+), 49 deletions(-) diff --git a/MMGridView/Classes/MMGridView.m b/MMGridView/Classes/MMGridView.m index f0a2bcf..a209311 100644 --- a/MMGridView/Classes/MMGridView.m +++ b/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)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]; } diff --git a/MMGridViewDemo/RootViewController.m b/MMGridViewDemo/RootViewController.m index 03e5b36..9364375 100644 --- a/MMGridViewDemo/RootViewController.m +++ b/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,13 +83,20 @@ - (void)reload [gridView reloadData]; } + +- (void)setupPageControl +{ + pageControl.numberOfPages = gridView.numberOfPages; + pageControl.currentPage = gridView.currentPageIndex; +} + // ---------------------------------------------------------------------------------- #pragma - MMGridViewDataSource - (NSInteger)numberOfCellsInGridView:(MMGridView *)gridView { - return 15; + return 42; } @@ -90,7 +104,6 @@ - (MMGridViewCell *)gridView:(MMGridView *)gridView cellAtIndex:(NSUInteger)inde { 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