Skip to content

Commit

Permalink
Use drawRect instead of layoutSubviews to draw the cells. Fixes reall…
Browse files Browse the repository at this point in the history
…y sloppy performance and memory issues. Should all be good now.
  • Loading branch information
René Sprotte committed Jul 20, 2011
1 parent 98e8c85 commit 11c2330
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 49 deletions.
81 changes: 36 additions & 45 deletions MMGridView/Classes/MMGridView.m
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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];
}


Expand Down
20 changes: 16 additions & 4 deletions MMGridViewDemo/RootViewController.m
Expand Up @@ -26,6 +26,7 @@

@interface RootViewController()
- (void)reload;
- (void)setupPageControl;
@end

@implementation RootViewController
Expand All @@ -41,6 +42,7 @@ - (void)dealloc
[super dealloc];
}


- (void)viewDidUnload {
[gridView release];
gridView = nil;
Expand All @@ -49,6 +51,7 @@ - (void)viewDidUnload {
[super viewDidUnload];
}


- (void)viewDidLoad
{
// Give us a nice title
Expand All @@ -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
Expand All @@ -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;
}
Expand Down Expand Up @@ -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.