Permalink
Browse files

KKGridViewDelegate now inheritances from UIScrollViewDelegate.

  • Loading branch information...
blommegard committed Apr 13, 2012
1 parent 8d0fe2c commit d254d3c926621a5585046c750cb2bf75bd6efb08
Showing with 41 additions and 43 deletions.
  1. +28 −30 KKGridView/KKGridView.h
  2. +12 −12 KKGridView/KKGridView.m
  3. +1 −1 KKGridView/KKGridViewController.m
View
@@ -29,8 +29,33 @@ typedef enum {
KKGridViewAnimationNone
} KKGridViewAnimation;
-@protocol KKGridViewDataSource;
-@protocol KKGridViewDelegate;
+@class KKGridView;
+
+@protocol KKGridViewDataSource <NSObject>
+@required
+- (NSUInteger)gridView:(KKGridView *)gridView numberOfItemsInSection:(NSUInteger)section;
+- (KKGridViewCell *)gridView:(KKGridView *)gridView cellForItemAtIndexPath:(KKIndexPath *)indexPath;
+@optional
+- (NSUInteger)numberOfSectionsInGridView:(KKGridView *)gridView;
+- (NSString *)gridView:(KKGridView *)gridView titleForHeaderInSection:(NSUInteger)section;
+- (NSString *)gridView:(KKGridView *)gridView titleForFooterInSection:(NSUInteger)section;
+- (CGFloat)gridView:(KKGridView *)gridView heightForHeaderInSection:(NSUInteger)section;
+- (CGFloat)gridView:(KKGridView *)gridView heightForFooterInSection:(NSUInteger)section;
+- (UIView *)gridView:(KKGridView *)gridView viewForHeaderInSection:(NSUInteger)section;
+- (UIView *)gridView:(KKGridView *)gridView viewForFooterInSection:(NSUInteger)section;
+- (UIView *)gridView:(KKGridView *)gridView viewForRow:(NSUInteger)row inSection:(NSUInteger)section; // a row is compromised of however many cells fit in a column of a given section
+- (NSArray *)sectionIndexTitlesForGridView:(KKGridView *)gridView;
+- (NSInteger)gridView:(KKGridView *)gridView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index;
+@end
+
+@protocol KKGridViewDelegate <NSObject, UIScrollViewDelegate>
+@optional
+- (void)gridView:(KKGridView *)gridView didSelectItemAtIndexPath:(KKIndexPath *)indexPath;
+- (void)gridView:(KKGridView *)gridView didDeselectItemAtIndexPath:(KKIndexPath *)indexPath;
+- (KKIndexPath *)gridView:(KKGridView *)gridView willSelectItemAtIndexPath:(KKIndexPath *)indexPath;
+- (KKIndexPath *)gridView:(KKGridView *)gridView willDeselectItemAtIndexPath:(KKIndexPath *)indexPath;
+- (void)gridView:(KKGridView *)gridView willDisplayCell:(KKGridViewCell *)cell atIndexPath:(KKIndexPath *)indexPath;
+@end
@interface KKGridView : UIScrollView
@@ -47,7 +72,7 @@ typedef enum {
#pragma mark - Data Source and Delegate
@property (nonatomic, kk_weak) IBOutlet id <KKGridViewDataSource> dataSource;
-@property (nonatomic, kk_weak) IBOutlet id <KKGridViewDelegate> gridDelegate;
+@property (nonatomic, kk_weak) IBOutlet id <KKGridViewDelegate> delegate;
#pragma mark - Getters
@@ -90,30 +115,3 @@ typedef enum {
- (NSArray *)indexPathsForSelectedCells;
@end
-
-
-@protocol KKGridViewDataSource <NSObject>
-@required
-- (NSUInteger)gridView:(KKGridView *)gridView numberOfItemsInSection:(NSUInteger)section;
-- (KKGridViewCell *)gridView:(KKGridView *)gridView cellForItemAtIndexPath:(KKIndexPath *)indexPath;
-@optional
-- (NSUInteger)numberOfSectionsInGridView:(KKGridView *)gridView;
-- (NSString *)gridView:(KKGridView *)gridView titleForHeaderInSection:(NSUInteger)section;
-- (NSString *)gridView:(KKGridView *)gridView titleForFooterInSection:(NSUInteger)section;
-- (CGFloat)gridView:(KKGridView *)gridView heightForHeaderInSection:(NSUInteger)section;
-- (CGFloat)gridView:(KKGridView *)gridView heightForFooterInSection:(NSUInteger)section;
-- (UIView *)gridView:(KKGridView *)gridView viewForHeaderInSection:(NSUInteger)section;
-- (UIView *)gridView:(KKGridView *)gridView viewForFooterInSection:(NSUInteger)section;
-- (UIView *)gridView:(KKGridView *)gridView viewForRow:(NSUInteger)row inSection:(NSUInteger)section; // a row is compromised of however many cells fit in a column of a given section
-- (NSArray *)sectionIndexTitlesForGridView:(KKGridView *)gridView;
-- (NSInteger)gridView:(KKGridView *)gridView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index;
-@end
-
-@protocol KKGridViewDelegate <NSObject>
-@optional
-- (void)gridView:(KKGridView *)gridView didSelectItemAtIndexPath:(KKIndexPath *)indexPath;
-- (void)gridView:(KKGridView *)gridView didDeselectItemAtIndexPath:(KKIndexPath *)indexPath;
-- (KKIndexPath *)gridView:(KKGridView *)gridView willSelectItemAtIndexPath:(KKIndexPath *)indexPath;
-- (KKIndexPath *)gridView:(KKGridView *)gridView willDeselectItemAtIndexPath:(KKIndexPath *)indexPath;
-- (void)gridView:(KKGridView *)gridView willDisplayCell:(KKGridViewCell *)cell atIndexPath:(KKIndexPath *)indexPath;
-@end
View
@@ -22,7 +22,7 @@
NSUInteger itemCount;
};
-@interface KKGridView () <UIGestureRecognizerDelegate,UIScrollViewDelegate> {
+@interface KKGridView () <UIGestureRecognizerDelegate> {
// View-wrapper containers
NSMutableArray *_footerViews;
NSMutableArray *_rowViews;
@@ -135,7 +135,7 @@ + (void)animateIf:(BOOL)animated delay:(NSTimeInterval)delay options:(UIViewAnim
@implementation KKGridView
@synthesize dataSource = _dataSource;
-@synthesize gridDelegate = _gridDelegate;
+@dynamic delegate;
@synthesize allowsMultipleSelection = _allowsMultipleSelection;
@synthesize cellPadding = _cellPadding;
@synthesize cellSize = _cellSize;
@@ -242,12 +242,12 @@ - (void)setDataSource:(id<KKGridViewDataSource>)dataSource
}
}
-- (void)setGridDelegate:(id<KKGridViewDelegate>)gridDelegate
+- (void)setDelegate:(id<KKGridViewDelegate>)delegate
{
- if (gridDelegate != _gridDelegate)
+ if (delegate != self.delegate)
{
- _gridDelegate = gridDelegate;
-#define RESPONDS_TO(sel) [_gridDelegate respondsToSelector:@selector(sel)]
+ [super setDelegate:delegate];
+#define RESPONDS_TO(sel) [self.delegate respondsToSelector:@selector(sel)]
_delegateRespondsTo.didSelectItem = RESPONDS_TO(gridView:didSelectItemAtIndexPath:);
_delegateRespondsTo.willSelectItem = RESPONDS_TO(gridView:willSelectItemAtIndexPath:);
_delegateRespondsTo.didDeselectItem = RESPONDS_TO(gridView:didDeselectItemAtIndexPath:);
@@ -678,7 +678,7 @@ - (CGFloat)_sectionHeightsCombinedUpToRow:(NSUInteger)row inSection:(NSUInteger)
- (void)_displayCell:(KKGridViewCell *)cell atIndexPath:(KKIndexPath *)indexPath withAnimation:(KKGridViewAnimation)animation
{
if (_delegateRespondsTo.willDisplayCell) {
- [_gridDelegate gridView:self willDisplayCell:cell atIndexPath:indexPath];
+ [self.delegate gridView:self willDisplayCell:cell atIndexPath:indexPath];
}
if ([_updateStack hasUpdateForIndexPath:indexPath]) {
@@ -1410,7 +1410,7 @@ - (void)_selectItemAtIndexPath:(KKIndexPath *)indexPath
}
if (_delegateRespondsTo.didSelectItem) {
- [_gridDelegate gridView:self didSelectItemAtIndexPath:indexPath];
+ [self.delegate gridView:self didSelectItemAtIndexPath:indexPath];
}
}
@@ -1424,7 +1424,7 @@ - (void)_deselectAll
if(_delegateRespondsTo.willDeselectItem)
{
- [_gridDelegate gridView:self willDeselectItemAtIndexPath:indexPath];
+ [self.delegate gridView:self willDeselectItemAtIndexPath:indexPath];
}
}
@@ -1434,7 +1434,7 @@ - (void)_deselectAll
- (void)_deselectItemAtIndexPath:(KKIndexPath *)indexPath
{
if (_selectedIndexPaths.count > 0 && _delegateRespondsTo.willDeselectItem && indexPath.index != NSNotFound && indexPath.section != NSNotFound) {
- KKIndexPath *redirectedPath = [_gridDelegate gridView:self willDeselectItemAtIndexPath:indexPath];
+ KKIndexPath *redirectedPath = [self.delegate gridView:self willDeselectItemAtIndexPath:indexPath];
if (redirectedPath != nil && ![redirectedPath isEqual:indexPath]) {
indexPath = redirectedPath;
}
@@ -1447,7 +1447,7 @@ - (void)_deselectItemAtIndexPath:(KKIndexPath *)indexPath
}
if (_delegateRespondsTo.didDeselectItem) {
- [_gridDelegate gridView:self didDeselectItemAtIndexPath:indexPath];
+ [self.delegate gridView:self didDeselectItemAtIndexPath:indexPath];
}
}
@@ -1487,7 +1487,7 @@ - (void)_handleSelection:(UILongPressGestureRecognizer *)recognizer
KKIndexPath *indexPath = [self indexPathForItemAtPoint:locationInSelf];
if (state == UIGestureRecognizerStateEnded && _delegateRespondsTo.willSelectItem)
- indexPath = [_gridDelegate gridView:self willSelectItemAtIndexPath:indexPath];
+ indexPath = [self.delegate gridView:self willSelectItemAtIndexPath:indexPath];
if (!indexPath || indexPath.index == NSNotFound || indexPath.section == NSNotFound) {
[self _cancelHighlighting];
@@ -19,7 +19,7 @@ - (void)loadView
_gridView = [[KKGridView alloc] initWithFrame:self.view.bounds];
_gridView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
_gridView.dataSource = self;
- _gridView.gridDelegate = self;
+ _gridView.delegate = self;
self.view = _gridView;
}

5 comments on commit d254d3c

Contributor

zadr replied Apr 13, 2012

I agree, awesome change! :D

However, why access the delegate through a property now, instead of direct ivar access like elsewhere?

And, I think that it would make sense to keep the gridDelegate property around (where -setGridDelegate: does nothing but call -setDelegate: on self) and marked as deprecated (with __attribute__((deprecated))), instead of getting rid of it completely. This way, people who update KKGridView won't suddenly find their build broken.

Collaborator

jonsterling replied Apr 14, 2012

Contributor

blommegard replied Apr 14, 2012

I'm with @jonsterling on this one, it's still in an early development phrase.

Contributor

zadr replied Apr 14, 2012

The phase of development doesn't matter much, if people are using it in the wild.

Collaborator

jonsterling replied Apr 14, 2012

Please sign in to comment.