diff --git a/.gitignore b/.gitignore index 07e6ab8..1d42b71 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,2 @@ .DS_Store -*.xcodeproj -*~.nib *.swp \ No newline at end of file diff --git a/MMGridView.xcodeproj/project.pbxproj b/MMGridView.xcodeproj/project.pbxproj index 24d900c..eefdd77 100644 --- a/MMGridView.xcodeproj/project.pbxproj +++ b/MMGridView.xcodeproj/project.pbxproj @@ -240,6 +240,7 @@ 7E6EF36F1340A48600DD91EC /* Project object */ = { isa = PBXProject; attributes = { + LastUpgradeCheck = 0430; ORGANIZATIONNAME = metaminded; }; buildConfigurationList = 7E6EF3721340A48600DD91EC /* Build configuration list for PBXProject "MMGridView" */; @@ -381,6 +382,7 @@ DSTROOT = /tmp/MMGridView.dst; GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = "MMGridView/MMGridView-Prefix.pch"; + GCC_VERSION = com.apple.compilers.llvm.clang.1_0; OTHER_LDFLAGS = "-ObjC"; PRODUCT_NAME = "$(TARGET_NAME)"; }; @@ -393,6 +395,7 @@ DSTROOT = /tmp/MMGridView.dst; GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = "MMGridView/MMGridView-Prefix.pch"; + GCC_VERSION = com.apple.compilers.llvm.clang.1_0; OTHER_LDFLAGS = "-ObjC"; PRODUCT_NAME = "$(TARGET_NAME)"; }; @@ -407,6 +410,7 @@ GCC_DYNAMIC_NO_PIC = NO; GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = "MMGridViewDemo/MMGridViewDemo-Prefix.pch"; + GCC_VERSION = com.apple.compilers.llvm.clang.1_0; INFOPLIST_FILE = "MMGridViewDemo/MMGridViewDemo-Info.plist"; IPHONEOS_DEPLOYMENT_TARGET = 4.3; OTHER_LDFLAGS = ""; @@ -424,6 +428,7 @@ COPY_PHASE_STRIP = YES; GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = "MMGridViewDemo/MMGridViewDemo-Prefix.pch"; + GCC_VERSION = com.apple.compilers.llvm.clang.1_0; INFOPLIST_FILE = "MMGridViewDemo/MMGridViewDemo-Info.plist"; IPHONEOS_DEPLOYMENT_TARGET = 4.3; OTHER_CFLAGS = "-DNS_BLOCK_ASSERTIONS=1"; diff --git a/MMGridView.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/MMGridView.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..003fa19 --- /dev/null +++ b/MMGridView.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/MMGridView.xcodeproj/project.xcworkspace/xcuserdata/Loocor.xcuserdatad/UserInterfaceState.xcuserstate b/MMGridView.xcodeproj/project.xcworkspace/xcuserdata/Loocor.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..69d39f0 Binary files /dev/null and b/MMGridView.xcodeproj/project.xcworkspace/xcuserdata/Loocor.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/MMGridView.xcodeproj/project.xcworkspace/xcuserdata/Loocor.xcuserdatad/WorkspaceSettings.xcsettings b/MMGridView.xcodeproj/project.xcworkspace/xcuserdata/Loocor.xcuserdatad/WorkspaceSettings.xcsettings new file mode 100644 index 0000000..bfffcfe --- /dev/null +++ b/MMGridView.xcodeproj/project.xcworkspace/xcuserdata/Loocor.xcuserdatad/WorkspaceSettings.xcsettings @@ -0,0 +1,10 @@ + + + + + HasAskedToTakeAutomaticSnapshotBeforeSignificantChanges + + SnapshotAutomaticallyBeforeSignificantChanges + + + diff --git a/MMGridView.xcodeproj/project.xcworkspace/xcuserdata/cokecoffe.xcuserdatad/UserInterfaceState.xcuserstate b/MMGridView.xcodeproj/project.xcworkspace/xcuserdata/cokecoffe.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..32edc00 Binary files /dev/null and b/MMGridView.xcodeproj/project.xcworkspace/xcuserdata/cokecoffe.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/MMGridView.xcodeproj/xcuserdata/Loocor.xcuserdatad/xcschemes/MMGridView.xcscheme b/MMGridView.xcodeproj/xcuserdata/Loocor.xcuserdatad/xcschemes/MMGridView.xcscheme new file mode 100644 index 0000000..37adf6a --- /dev/null +++ b/MMGridView.xcodeproj/xcuserdata/Loocor.xcuserdatad/xcschemes/MMGridView.xcscheme @@ -0,0 +1,58 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/MMGridView.xcodeproj/xcuserdata/Loocor.xcuserdatad/xcschemes/MMGridViewDemo.xcscheme b/MMGridView.xcodeproj/xcuserdata/Loocor.xcuserdatad/xcschemes/MMGridViewDemo.xcscheme new file mode 100644 index 0000000..4ced999 --- /dev/null +++ b/MMGridView.xcodeproj/xcuserdata/Loocor.xcuserdatad/xcschemes/MMGridViewDemo.xcscheme @@ -0,0 +1,85 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/MMGridView.xcodeproj/xcuserdata/Loocor.xcuserdatad/xcschemes/xcschememanagement.plist b/MMGridView.xcodeproj/xcuserdata/Loocor.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000..56219e9 --- /dev/null +++ b/MMGridView.xcodeproj/xcuserdata/Loocor.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,32 @@ + + + + + SchemeUserState + + MMGridView.xcscheme + + orderHint + 1 + + MMGridViewDemo.xcscheme + + orderHint + 0 + + + SuppressBuildableAutocreation + + 7E6EF3771340A48600DD91EC + + primary + + + 7E6EF3C71340B52E00DD91EC + + primary + + + + + diff --git a/MMGridView.xcodeproj/xcuserdata/cokecoffe.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist b/MMGridView.xcodeproj/xcuserdata/cokecoffe.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist new file mode 100644 index 0000000..05301bc --- /dev/null +++ b/MMGridView.xcodeproj/xcuserdata/cokecoffe.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist @@ -0,0 +1,5 @@ + + + diff --git a/MMGridView.xcodeproj/xcuserdata/cokecoffe.xcuserdatad/xcschemes/MMGridView.xcscheme b/MMGridView.xcodeproj/xcuserdata/cokecoffe.xcuserdatad/xcschemes/MMGridView.xcscheme new file mode 100644 index 0000000..9393aee --- /dev/null +++ b/MMGridView.xcodeproj/xcuserdata/cokecoffe.xcuserdatad/xcschemes/MMGridView.xcscheme @@ -0,0 +1,59 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/MMGridView.xcodeproj/xcuserdata/cokecoffe.xcuserdatad/xcschemes/MMGridViewDemo.xcscheme b/MMGridView.xcodeproj/xcuserdata/cokecoffe.xcuserdatad/xcschemes/MMGridViewDemo.xcscheme new file mode 100644 index 0000000..01d7300 --- /dev/null +++ b/MMGridView.xcodeproj/xcuserdata/cokecoffe.xcuserdatad/xcschemes/MMGridViewDemo.xcscheme @@ -0,0 +1,86 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/MMGridView.xcodeproj/xcuserdata/cokecoffe.xcuserdatad/xcschemes/xcschememanagement.plist b/MMGridView.xcodeproj/xcuserdata/cokecoffe.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000..0888ffd --- /dev/null +++ b/MMGridView.xcodeproj/xcuserdata/cokecoffe.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,32 @@ + + + + + SchemeUserState + + MMGridView.xcscheme + + orderHint + 0 + + MMGridViewDemo.xcscheme + + orderHint + 1 + + + SuppressBuildableAutocreation + + 7E6EF3771340A48600DD91EC + + primary + + + 7E6EF3C71340B52E00DD91EC + + primary + + + + + diff --git a/MMGridView/Classes/MMGridView.h b/MMGridView/Classes/MMGridView.h index 030051a..b7568e8 100644 --- a/MMGridView/Classes/MMGridView.h +++ b/MMGridView/Classes/MMGridView.h @@ -22,6 +22,10 @@ #import #import "MMGridViewCell.h" +typedef enum { + VerticalLayout, + HorizontalLayout +} GridLayoutStyle; @class MMGridView; @@ -32,6 +36,7 @@ @protocol MMGridViewDataSource - (NSInteger)numberOfCellsInGridView:(MMGridView *)gridView; - (MMGridViewCell *)gridView:(MMGridView *)gridView cellAtIndex:(NSUInteger)index; +- (void)loadMoreForGrid; @end // ---------------------------------------------------------------------------------- @@ -39,6 +44,7 @@ #pragma - MMGridViewDelegate @protocol MMGridViewDelegate +-(BOOL)canLoadMoreForGrid; @optional - (void)gridView:(MMGridView *)gridView didSelectCell:(MMGridViewCell *)cell atIndex:(NSUInteger)index; - (void)gridView:(MMGridView *)gridView didDoubleTapCell:(MMGridViewCell *)cell atIndex:(NSUInteger)index; @@ -58,6 +64,7 @@ NSUInteger numberOfRows; NSUInteger numberOfColumns; NSUInteger cellMargin; + GridLayoutStyle layoutStyle; } @property (nonatomic, retain, readonly) UIScrollView *scrollView; @@ -68,7 +75,9 @@ @property (nonatomic) NSUInteger cellMargin; @property (nonatomic, readonly) NSUInteger currentPageIndex; @property (nonatomic, readonly) NSUInteger numberOfPages; +@property (nonatomic, readonly) NSUInteger numberOfTatalRows; +@property (nonatomic) GridLayoutStyle layoutStyle; - (void)reloadData; - +- (void)loadMoreFinished; @end diff --git a/MMGridView/Classes/MMGridView.m b/MMGridView/Classes/MMGridView.m index ec09e50..54b127d 100644 --- a/MMGridView/Classes/MMGridView.m +++ b/MMGridView/Classes/MMGridView.m @@ -19,6 +19,13 @@ // OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // + +#define K_DEFAULT_NUMBEROFROWS 3 +#define K_DEFAULT_NUMBEROFCOLUMNS 2 +#define K_DEFAULT_CELLMARGIN 5 +#define K_DEFAULT_PAGEINDEX 0 +#define K_LOADMORE_HEIGHT 40 + #import "MMGridView.h" @@ -27,6 +34,8 @@ @interface MMGridView() @property (nonatomic, retain) UIScrollView *scrollView; @property (nonatomic) NSUInteger currentPageIndex; @property (nonatomic) NSUInteger numberOfPages; +@property (nonatomic) NSUInteger numberOfTatalRows; +@property (nonatomic) BOOL isLoadingMore; - (void)createSubviews; - (void)cellWasSelected:(MMGridViewCell *)cell; @@ -45,6 +54,8 @@ @implementation MMGridView @synthesize cellMargin; @synthesize currentPageIndex; @synthesize numberOfPages; +@synthesize numberOfTatalRows; +@synthesize layoutStyle; - (void)dealloc @@ -76,10 +87,12 @@ - (id)initWithCoder:(NSCoder *)aDecoder - (void)createSubviews { - cellMargin = 3; - numberOfRows = 3; - numberOfColumns = 2; - currentPageIndex = 0; + + cellMargin = K_DEFAULT_CELLMARGIN; + numberOfRows = K_DEFAULT_NUMBEROFROWS; + numberOfColumns = K_DEFAULT_NUMBEROFCOLUMNS; + currentPageIndex = K_DEFAULT_PAGEINDEX; + layoutStyle = VerticalLayout; self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; self.contentMode = UIViewContentModeRedraw; @@ -87,16 +100,26 @@ - (void)createSubviews self.scrollView = [[[UIScrollView alloc] initWithFrame:self.bounds] autorelease]; self.scrollView.delegate = self; - self.scrollView.backgroundColor = [UIColor clearColor]; + self.scrollView.backgroundColor = self.backgroundColor; 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]; + if (layoutStyle == HorizontalLayout) { + self.scrollView.pagingEnabled = YES; + self.scrollView.showsVerticalScrollIndicator = NO; + self.scrollView.alwaysBounceVertical = NO; + } else { + self.scrollView.pagingEnabled = NO; + self.scrollView.showsVerticalScrollIndicator = YES; + self.scrollView.alwaysBounceVertical = YES; + } + + [self addSubview:self.scrollView]; [self reloadData]; + + _isLoadingMore = NO; } @@ -107,18 +130,18 @@ - (void)drawRect:(CGRect)rect NSInteger noOfRows = self.numberOfRows; NSUInteger cellsPerPage = self.numberOfColumns * self.numberOfRows; - BOOL isLandscape = UIInterfaceOrientationIsLandscape([[UIDevice currentDevice] orientation]); - if (isLandscape) { - // In landscape mode switch rows and columns - noOfCols = self.numberOfRows; - noOfRows = self.numberOfColumns; - } - CGRect gridBounds = self.scrollView.bounds; CGRect cellBounds = CGRectMake(0, 0, gridBounds.size.width / (float)noOfCols, gridBounds.size.height / (float)noOfRows); - CGSize contentSize = CGSizeMake(self.numberOfPages * gridBounds.size.width, gridBounds.size.height); + CGSize contentSize; + + if (layoutStyle == HorizontalLayout) { + contentSize = CGSizeMake(self.numberOfPages * gridBounds.size.width, gridBounds.size.height); + } else { + contentSize = CGSizeMake(gridBounds.size.width, self.numberOfTatalRows * cellBounds.size.height); + } + [self.scrollView setContentSize:contentSize]; for (UIView *v in self.scrollView.subviews) { @@ -133,9 +156,15 @@ - (void)drawRect:(CGRect)rect 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)); - + CGPoint origin; + if (layoutStyle == HorizontalLayout) { + origin = CGPointMake((page * gridBounds.size.width) + ((i % noOfCols) * cellBounds.size.width), + (row * cellBounds.size.height)); + } else { + origin = CGPointMake((i % noOfCols) * cellBounds.size.width, + (ceil( i / noOfCols)) * cellBounds.size.height); + } + CGRect f = CGRectMake(origin.x, origin.y, cellBounds.size.width, cellBounds.size.height); cell.frame = CGRectInset(f, self.cellMargin, self.cellMargin); @@ -165,6 +194,27 @@ - (void)setNumberOfRows:(NSUInteger)value [self reloadData]; } +-(NSUInteger)numberOfRows +{ + UIInterfaceOrientation currentOrientation = [UIApplication sharedApplication].statusBarOrientation; + BOOL isLandscape = UIInterfaceOrientationIsLandscape(currentOrientation); + if (isLandscape) { + return numberOfColumns; + }else{ + return numberOfRows; + } +} + +-(NSUInteger)numberOfColumns +{ + UIInterfaceOrientation currentOrientation = [UIApplication sharedApplication].statusBarOrientation; + BOOL isLandscape = UIInterfaceOrientationIsLandscape(currentOrientation); + if (isLandscape) { + return numberOfRows; + }else{ + return numberOfColumns; + } +} - (void)setCellMargin:(NSUInteger)value { @@ -175,12 +225,32 @@ - (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)); + if (layoutStyle == HorizontalLayout) { + NSUInteger numberOfCells = [self.dataSource numberOfCellsInGridView:self]; + NSUInteger cellsPerPage = self.numberOfColumns * self.numberOfRows; + return (uint)(ceil((float)numberOfCells / (float)cellsPerPage)); + } else { + return 1; + } +} + + +- (NSUInteger)numberOfTatalRows +{ + if (layoutStyle == VerticalLayout) { + NSUInteger numberofCells = [self.dataSource numberOfCellsInGridView:self]; + if (numberofCells % self.numberOfColumns == 0) { + return numberofCells / self.numberOfColumns; + } else { + return numberofCells / self.numberOfColumns + 1; + } + } else { + return self.numberOfRows; + } } + - (void)reloadData { [self setNeedsDisplay]; @@ -229,4 +299,22 @@ - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView [self updateCurrentPageIndex]; } + +- (void) scrollViewDidScroll:(UIScrollView *)scrollView +{ + if (!_isLoadingMore && [delegate canLoadMoreForGrid]) { + CGFloat scrollPosition = self.scrollView.contentSize.height - self.scrollView.contentOffset.y- self.scrollView.frame.size.height; + if (scrollPosition < K_LOADMORE_HEIGHT) { + _isLoadingMore = YES; + [dataSource loadMoreForGrid]; + } + } +} + +-(void)loadMoreFinished +{ + _isLoadingMore = NO; +} + + @end diff --git a/MMGridView/MMGridViewDefaultCell.h b/MMGridView/MMGridViewDefaultCell.h index 37066c5..59c6cc4 100644 --- a/MMGridView/MMGridViewDefaultCell.h +++ b/MMGridView/MMGridViewDefaultCell.h @@ -28,6 +28,9 @@ UILabel *textLabel; UIView *textLabelBackgroundView; UIView *backgroundView; + + NSUInteger labelHeight; + NSUInteger labelInset; } @property (nonatomic, retain) UILabel *textLabel; diff --git a/MMGridView/MMGridViewDefaultCell.m b/MMGridView/MMGridViewDefaultCell.m index 71397cd..c6a50b2 100644 --- a/MMGridView/MMGridViewDefaultCell.m +++ b/MMGridView/MMGridViewDefaultCell.m @@ -19,8 +19,10 @@ // OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // -#import "MMGridViewDefaultCell.h" +#define K_DEFAULT_LABEL_HEIGHT 30 +#define K_DEFAULT_LABEL_INSET 5 +#import "MMGridViewDefaultCell.h" @implementation MMGridViewDefaultCell @@ -67,16 +69,16 @@ - (void)layoutSubviews { [super layoutSubviews]; - int labelHeight = 30; - int inset = 5; - + labelHeight = K_DEFAULT_LABEL_HEIGHT; + labelInset = K_DEFAULT_LABEL_INSET; + // Background view self.backgroundView.frame = self.bounds; self.backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; // Layout label self.textLabelBackgroundView.frame = CGRectMake(0, - self.bounds.size.height - labelHeight - inset, + self.bounds.size.height - labelHeight - labelInset, self.bounds.size.width, labelHeight); self.textLabelBackgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; @@ -86,7 +88,7 @@ - (void)layoutSubviews 0, self.textLabel.superview.bounds.size.width, self.textLabel.superview.bounds.size.height); - self.textLabel.frame = CGRectInset(f, inset, 0); + self.textLabel.frame = CGRectInset(f, labelInset, 0); self.textLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; } diff --git a/MMGridViewDemo/RootViewController.m b/MMGridViewDemo/RootViewController.m index 9364375..e6c9d5b 100644 --- a/MMGridViewDemo/RootViewController.m +++ b/MMGridViewDemo/RootViewController.m @@ -25,6 +25,11 @@ @interface RootViewController() +{ + +} +@property (retain,nonatomic) NSMutableArray *dataSource; + - (void)reload; - (void)setupPageControl; @end @@ -54,6 +59,11 @@ - (void)viewDidUnload { - (void)viewDidLoad { + self.dataSource = [NSMutableArray arrayWithCapacity:20]; + for (int i = 0; i<20; i++) { + [_dataSource addObject:@"Example"]; + } + // Give us a nice title self.title = @"MMGridView Demo"; @@ -64,8 +74,16 @@ - (void)viewDidLoad self.navigationItem.rightBarButtonItem = reloadButton; [reloadButton release]; + // setup MMGrid view + + gridView.cellMargin = 5; + gridView.numberOfRows = 4; + gridView.numberOfColumns = 3; + // gridView.layoutStyle = HorizontalLayout; + gridView.layoutStyle = VerticalLayout; + // setup the page control - [self setupPageControl]; + [self setupPageControl]; } @@ -96,7 +114,7 @@ - (void)setupPageControl - (NSInteger)numberOfCellsInGridView:(MMGridView *)gridView { - return 42; + return [_dataSource count]; } @@ -108,7 +126,23 @@ - (MMGridViewCell *)gridView:(MMGridView *)gridView cellAtIndex:(NSUInteger)inde return cell; } -// ---------------------------------------------------------------------------------- +-(void)loadMoreForGrid +{ + //request more data here. + for (int i = 0; i<12; i++) { + [_dataSource addObject:@"Example"]; + } + + [self performSelector:@selector(LoadDataFinished) withObject:nil afterDelay:1.0]; +} + +-(void)LoadDataFinished +{ + [gridView reloadData]; + [gridView loadMoreFinished]; +} + + // ---------------------------------------------------------------------------------- #pragma - MMGridViewDelegate @@ -137,4 +171,11 @@ - (void)gridView:(MMGridView *)theGridView changedPageToIndex:(NSUInteger)index [self setupPageControl]; } +-(BOOL)canLoadMoreForGrid +{ + //return hasMore. + //here assume always YES. + return YES; +} + @end