Skip to content

Commit

Permalink
Added properties to see the numberOfPages and the currentPageIndex. A…
Browse files Browse the repository at this point in the history
…dded delegate method to know if the current page has changed.
  • Loading branch information
René Sprotte committed Apr 10, 2011
1 parent 60d7cff commit c5d2c81
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 13 deletions.
11 changes: 7 additions & 4 deletions MMGridView/Classes/MMGridView.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

@protocol MMGridViewDataSource<NSObject>
- (NSInteger)numberOfCellsInGridView:(MMGridView *)gridView;
- (MMGridViewCell *)gridView:(MMGridView *)gridView cellAtIndex:(NSInteger)index;
- (MMGridViewCell *)gridView:(MMGridView *)gridView cellAtIndex:(NSUInteger)index;
@end

// ----------------------------------------------------------------------------------
Expand All @@ -40,15 +40,16 @@

@protocol MMGridViewDelegate<NSObject>
@optional
- (void)gridView:(MMGridView *)gridView didSelectCell:(MMGridViewCell *)cell atIndex:(NSInteger)index;
- (void)gridView:(MMGridView *)gridView didDoubleTappedCell:(MMGridViewCell *)cell atIndex:(NSInteger)index;
- (void)gridView:(MMGridView *)gridView didSelectCell:(MMGridViewCell *)cell atIndex:(NSUInteger)index;
- (void)gridView:(MMGridView *)gridView didDoubleTappedCell:(MMGridViewCell *)cell atIndex:(NSUInteger)index;
- (void)gridView:(MMGridView *)gridView changedPageToIndex:(NSUInteger)index;
@end

// ----------------------------------------------------------------------------------

#pragma - MMGridView

@interface MMGridView : UIView
@interface MMGridView : UIView<UIScrollViewDelegate>
{
@private
UIScrollView *scrollView;
Expand All @@ -65,6 +66,8 @@
@property (nonatomic) NSUInteger numberOfRows;
@property (nonatomic) NSUInteger numberOfColumns;
@property (nonatomic) NSUInteger cellMargin;
@property (nonatomic, readonly) NSUInteger currentPageIndex;
@property (nonatomic, readonly) NSUInteger numberOfPages;

- (void)reloadData;

Expand Down
45 changes: 39 additions & 6 deletions MMGridView/Classes/MMGridView.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@
@interface MMGridView()

@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic) NSUInteger currentPageIndex;
@property (nonatomic) NSUInteger numberOfPages;

- (void)createSubviews;
- (void)cellWasSelected:(MMGridViewCell *)cell;
- (void)cellWasDoubleTapped:(MMGridViewCell *)cell;
- (void)updateCurrentPageIndex;
@end


Expand All @@ -40,6 +43,8 @@ @implementation MMGridView
@synthesize numberOfRows;
@synthesize numberOfColumns;
@synthesize cellMargin;
@synthesize currentPageIndex;
@synthesize numberOfPages;


- (void)dealloc
Expand Down Expand Up @@ -77,10 +82,12 @@ - (void)createSubviews
self.cellMargin = 3;
self.numberOfRows = 3;
self.numberOfColumns = 2;
self.currentPageIndex = 0;

self.backgroundColor = [UIColor clearColor];

self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectNull];
self.scrollView.delegate = self;
self.scrollView.backgroundColor = [UIColor clearColor];
self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.scrollView.alwaysBounceHorizontal = NO;
Expand Down Expand Up @@ -123,6 +130,14 @@ - (void)setCellMargin:(NSUInteger)value
}


- (NSUInteger)numberOfPages
{
NSUInteger numberOfCells = [self.dataSource numberOfCellsInGridView:self];
NSUInteger cellsPerPage = self.numberOfColumns * self.numberOfRows;
return (uint)(ceil((float)numberOfCells / (float)cellsPerPage));
}


- (void)layoutSubviews
{
self.scrollView.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
Expand All @@ -139,7 +154,7 @@ - (void)reloadData
if (self.dataSource) {
NSInteger noOfCols = self.numberOfColumns;
NSInteger noOfRows = self.numberOfRows;
NSInteger cellsPerPage = noOfCols * noOfRows;
NSUInteger cellsPerPage = self.numberOfColumns * self.numberOfRows;

BOOL isLandscape = UIInterfaceOrientationIsLandscape([[UIDevice currentDevice] orientation]);
if (isLandscape) {
Expand All @@ -148,17 +163,14 @@ - (void)reloadData
noOfRows = self.numberOfColumns;
}

NSInteger numberOfCells = [self.dataSource numberOfCellsInGridView:self];
NSInteger numberOfPages = (int)(ceil((float)numberOfCells / (float)cellsPerPage));

CGRect gridBounds = self.scrollView.bounds;
CGRect cellBounds = CGRectMake(0, 0, gridBounds.size.width / (float)noOfCols,
gridBounds.size.height / (float)noOfRows);

CGSize contentSize = CGSizeMake(numberOfPages * gridBounds.size.width, gridBounds.size.height);
CGSize contentSize = CGSizeMake(self.numberOfPages * gridBounds.size.width, gridBounds.size.height);
[self.scrollView setContentSize:contentSize];

for (NSInteger i = 0; i < numberOfCells; i++) {
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]];
Expand Down Expand Up @@ -194,4 +206,25 @@ - (void)cellWasDoubleTapped:(MMGridViewCell *)cell
}
}


- (void)updateCurrentPageIndex
{
CGFloat pageWidth = scrollView.frame.size.width;
NSUInteger cpi = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.currentPageIndex = cpi;

if (delegate) {
[self.delegate gridView:self changedPageToIndex:self.currentPageIndex];
}
}

// ----------------------------------------------------------------------------------

#pragma - UIScrollViewDelegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[self updateCurrentPageIndex];
}

@end
13 changes: 10 additions & 3 deletions MMGridViewDemo/RootViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ - (NSInteger)numberOfCellsInGridView:(MMGridView *)gridView
}


- (MMGridViewCell *)gridView:(MMGridView *)gridView cellAtIndex:(NSInteger)index
- (MMGridViewCell *)gridView:(MMGridView *)gridView cellAtIndex:(NSUInteger)index
{
MMGridViewCell *cell = [[[MMGridViewCell alloc] initWithFrame:CGRectNull] autorelease];
cell.textLabel.text = [NSString stringWithFormat:@"Cell %d", index];
Expand All @@ -81,15 +81,15 @@ - (MMGridViewCell *)gridView:(MMGridView *)gridView cellAtIndex:(NSInteger)index

#pragma - MMGridViewDelegate

- (void)gridView:(MMGridView *)gridView didSelectCell:(MMGridViewCell *)cell atIndex:(NSInteger)index
- (void)gridView:(MMGridView *)gridView didSelectCell:(MMGridViewCell *)cell atIndex:(NSUInteger)index
{
AnyViewController *c = [[AnyViewController alloc] initWithNibName:@"AnyViewController" bundle:nil];
[self.navigationController pushViewController:c animated:YES];
[c release];
}


- (void)gridView:(MMGridView *)gridView didDoubleTappedCell:(MMGridViewCell *)cell atIndex:(NSInteger)index
- (void)gridView:(MMGridView *)gridView didDoubleTappedCell:(MMGridViewCell *)cell atIndex:(NSUInteger)index
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:[NSString stringWithFormat:@"Cell at index %d was double tapped.", index]
Expand All @@ -100,4 +100,11 @@ - (void)gridView:(MMGridView *)gridView didDoubleTappedCell:(MMGridViewCell *)ce
[alert release];
}


- (void)gridView:(MMGridView *)theGridView changedPageToIndex:(NSUInteger)index
{
NSLog(@"GridView changed page to index: %d", index);
NSLog(@"GridView has %d pages", theGridView.numberOfPages);
}

@end

0 comments on commit c5d2c81

Please sign in to comment.