Skip to content
Browse files

Unblockified delegate

  • Loading branch information...
1 parent 8f150c4 commit 3b798d1b3a15f1b58f46dc579a59377c9f4c6423 @jonsterling jonsterling committed Dec 16, 2011
Showing with 43 additions and 30 deletions.
  1. +11 −12 KKGridView/KKGridView.h
  2. +32 −18 KKGridView/KKGridView.m
View
23 KKGridView/KKGridView.h
@@ -10,10 +10,6 @@
#import <KKGridView/KKIndexPath.h>
#import <KKGridView/Definitions.h>
-typedef void (^KKGridViewIndexPath)(KKGridView *gridView, KKIndexPath *indexPath);
-typedef KKIndexPath * (^KKGridViewReturnPath)(KKGridView *gridView, KKIndexPath *indexPath);
-typedef void (^KKGridViewWillDisplayCellAtPath)(KKGridView *gridView, KKGridViewCell *cell, KKIndexPath *indexPath);
-
typedef enum {
KKGridViewScrollPositionNone,
KKGridViewScrollPositionTop,
@@ -34,6 +30,7 @@ typedef enum {
} KKGridViewAnimation;
@protocol KKGridViewDataSource;
+@protocol KKGridViewDelegate;
@interface KKGridView : UIScrollView
@@ -50,14 +47,7 @@ typedef enum {
#pragma mark - Data Source and Delegate
@property (nonatomic, __kk_weak) id <KKGridViewDataSource> dataSource;
-
-#pragma mark - Delegate
-
-@property (nonatomic, copy) KKGridViewIndexPath didSelectIndexPathBlock;
-@property (nonatomic, copy) KKGridViewReturnPath willSelectItemAtIndexPathBlock;
-@property (nonatomic, copy) KKGridViewReturnPath willDeselectItemAtIndexPathBlock;
-@property (nonatomic, copy) KKGridViewIndexPath didDeselectIndexPathBlock;
-@property (nonatomic, copy) KKGridViewWillDisplayCellAtPath willDisplayCellAtPathBlock;
+@property (nonatomic, __kk_weak) id <KKGridViewDelegate> gridDelegate;
#pragma mark - Getters
@@ -106,4 +96,13 @@ typedef enum {
- (CGFloat)gridView:(KKGridView *)gridView heightForFooterInSection:(NSUInteger)section;
- (UIView *)gridView:(KKGridView *)gridView viewForHeaderInSection:(NSUInteger)section;
- (UIView *)gridView:(KKGridView *)gridView viewForFooterInSection:(NSUInteger)section;
+@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
50 KKGridView/KKGridView.m
@@ -53,6 +53,14 @@ @interface KKGridView () {
unsigned int viewForHeader:1;
unsigned int viewForFooter:1;
} _dataSourceRespondsTo;
+
+ struct {
+ unsigned int didSelectItem:1;
+ unsigned int willSelectItem:1;
+ unsigned int didDeselectItem:1;
+ unsigned int willDeselectItem:1;
+ unsigned int willDisplayCell:1;
+ } _delegateRespondsTo;
}
// Initialization
@@ -101,8 +109,8 @@ - (void)_handleSelection:(UITapGestureRecognizer *)recognizer;
@implementation KKGridView
-// Main Properties
@synthesize dataSource = _dataSource;
+@synthesize gridDelegate = _gridDelegate;
@synthesize allowsMultipleSelection = _allowsMultipleSelection;
@synthesize cellPadding = _cellPadding;
@synthesize cellSize = _cellSize;
@@ -112,13 +120,6 @@ @implementation KKGridView
@synthesize numberOfSections = _numberOfSections;
@synthesize backgroundView = _backgroundView;
-// Delegate Blocks
-@synthesize didSelectIndexPathBlock = _didSelectIndexPathBlock;
-@synthesize willSelectItemAtIndexPathBlock = _willSelectItemAtIndexPathBlock;
-@synthesize willDeselectItemAtIndexPathBlock = _willDeselectItemAtIndexPathBlock;
-@synthesize didDeselectIndexPathBlock = _didDeselectIndexPathBlock;
-@synthesize willDisplayCellAtPathBlock = _willDisplayCellAtPathBlock;
-
#pragma mark - Initialization Methods
- (id)init
@@ -195,6 +196,19 @@ - (void)setDataSource:(id<KKGridViewDataSource>)dataSource
}
}
+- (void)setGridDelegate:(id<KKGridViewDelegate>)gridDelegate
+{
+ if (gridDelegate != _gridDelegate)
+ {
+ _gridDelegate = gridDelegate;
+ _delegateRespondsTo.didSelectItem = [_gridDelegate respondsToSelector:@selector(gridView:didSelectItemAtIndexPath:)];
+ _delegateRespondsTo.willSelectItem = [_gridDelegate respondsToSelector:@selector(gridView:willSelectItemAtIndexPath:)];
+ _delegateRespondsTo.didDeselectItem = [_gridDelegate respondsToSelector:@selector(gridView:didDeselectItemAtIndexPath:)];
+ _delegateRespondsTo.willDeselectItem = [_gridDelegate respondsToSelector:@selector(gridView:willDeselectItemAtIndexPath:)];
+ _delegateRespondsTo.willDisplayCell = [_gridDelegate respondsToSelector:@selector(gridView:willDisplayCellAtIndexPath:)];
+ }
+}
+
- (void)setFrame:(CGRect)frame
{
CGRect oldFrame = self.frame;
@@ -585,8 +599,8 @@ - (CGFloat)_heightForSection:(NSUInteger)section
- (void)_displayCell:(KKGridViewCell *)cell atIndexPath:(KKIndexPath *)indexPath withAnimation:(KKGridViewAnimation)animation
{
- if (_willDisplayCellAtPathBlock) {
- _willDisplayCellAtPathBlock(self, cell, indexPath);
+ if (_delegateRespondsTo.willDisplayCell) {
+ [_gridDelegate gridView:self willDisplayCell:cell atIndexPath:indexPath];
}
if ([_updateStack hasUpdateForIndexPath:indexPath]) {
@@ -1136,15 +1150,15 @@ - (void)_selectItemAtIndexPath:(KKIndexPath *)indexPath
[_selectedIndexPaths addObject:indexPath];
cell.selected = YES;
}
- if (_didSelectIndexPathBlock) {
- _didSelectIndexPathBlock(self, indexPath);
+ if (_delegateRespondsTo.didSelectItem) {
+ [_gridDelegate gridView:self didSelectItemAtIndexPath:indexPath];
}
}
- (void)_deselectItemAtIndexPath:(KKIndexPath *)indexPath
{
- if (_selectedIndexPaths.count > 0 && _willDeselectItemAtIndexPathBlock) {
- KKIndexPath *redirectedPath = _willDeselectItemAtIndexPathBlock(self, indexPath);
+ if (_selectedIndexPaths.count > 0 && _delegateRespondsTo.willDeselectItem) {
+ KKIndexPath *redirectedPath = [_gridDelegate gridView:self willDeselectItemAtIndexPath:indexPath];
if (redirectedPath != nil && ![redirectedPath isEqual:indexPath]) {
indexPath = redirectedPath ? redirectedPath : indexPath;
}
@@ -1156,8 +1170,8 @@ - (void)_deselectItemAtIndexPath:(KKIndexPath *)indexPath
cell.selected = NO;
}
- if (_didDeselectIndexPathBlock) {
- _didDeselectIndexPathBlock(self, indexPath);
+ if (_delegateRespondsTo.didDeselectItem) {
+ [_gridDelegate gridView:self didDeselectItemAtIndexPath:indexPath];
}
}
@@ -1166,8 +1180,8 @@ - (void)_deselectItemAtIndexPath:(KKIndexPath *)indexPath
- (void)_handleSelection:(UITapGestureRecognizer *)recognizer
{
KKIndexPath *indexPath = [self indexPathsForItemAtPoint:[recognizer locationInView:self]];
- if (_willSelectItemAtIndexPathBlock)
- indexPath = _willSelectItemAtIndexPathBlock(self, indexPath);
+ if (_delegateRespondsTo.willSelectItem)
+ indexPath = [_gridDelegate gridView:self willSelectItemAtIndexPath:indexPath];
if (indexPath.index == NSNotFound || indexPath.section == NSNotFound)
return;

0 comments on commit 3b798d1

Please sign in to comment.
Something went wrong with that request. Please try again.