Permalink
Browse files

Added properties to see the numberOfPages and the currentPageIndex. A…

…dded delegate method to know if the current page has changed.
  • Loading branch information...
René Sprotte
René Sprotte committed Apr 10, 2011
1 parent 60d7cff commit c5d2c813dadc0eb688458e4a36505bb413451fab
Showing with 56 additions and 13 deletions.
  1. +7 −4 MMGridView/Classes/MMGridView.h
  2. +39 −6 MMGridView/Classes/MMGridView.m
  3. +10 −3 MMGridViewDemo/RootViewController.m
@@ -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
// ----------------------------------------------------------------------------------
@@ -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;
@@ -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;
@@ -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
@@ -40,6 +43,8 @@ @implementation MMGridView
@synthesize numberOfRows;
@synthesize numberOfColumns;
@synthesize cellMargin;
+@synthesize currentPageIndex;
+@synthesize numberOfPages;
- (void)dealloc
@@ -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;
@@ -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);
@@ -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) {
@@ -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]];
@@ -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
@@ -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];
@@ -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]
@@ -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.