Skip to content
This repository
Browse code

KKGridViewDelegate now inheritances from UIScrollViewDelegate.

  • Loading branch information...
commit d254d3c926621a5585046c750cb2bf75bd6efb08 1 parent 8d0fe2c
Simon Blommegård authored April 13, 2012
58  KKGridView/KKGridView.h
@@ -29,8 +29,33 @@ typedef enum {
29 29
     KKGridViewAnimationNone
30 30
 } KKGridViewAnimation;
31 31
 
32  
-@protocol KKGridViewDataSource;
33  
-@protocol KKGridViewDelegate;
  32
+@class KKGridView;
  33
+
  34
+@protocol KKGridViewDataSource <NSObject>
  35
+@required
  36
+- (NSUInteger)gridView:(KKGridView *)gridView numberOfItemsInSection:(NSUInteger)section;
  37
+- (KKGridViewCell *)gridView:(KKGridView *)gridView cellForItemAtIndexPath:(KKIndexPath *)indexPath;
  38
+@optional
  39
+- (NSUInteger)numberOfSectionsInGridView:(KKGridView *)gridView;
  40
+- (NSString *)gridView:(KKGridView *)gridView titleForHeaderInSection:(NSUInteger)section;
  41
+- (NSString *)gridView:(KKGridView *)gridView titleForFooterInSection:(NSUInteger)section;
  42
+- (CGFloat)gridView:(KKGridView *)gridView heightForHeaderInSection:(NSUInteger)section;
  43
+- (CGFloat)gridView:(KKGridView *)gridView heightForFooterInSection:(NSUInteger)section;
  44
+- (UIView *)gridView:(KKGridView *)gridView viewForHeaderInSection:(NSUInteger)section;
  45
+- (UIView *)gridView:(KKGridView *)gridView viewForFooterInSection:(NSUInteger)section;
  46
+- (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
  47
+- (NSArray *)sectionIndexTitlesForGridView:(KKGridView *)gridView;
  48
+- (NSInteger)gridView:(KKGridView *)gridView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index;
  49
+@end
  50
+
  51
+@protocol KKGridViewDelegate <NSObject, UIScrollViewDelegate>
  52
+@optional
  53
+- (void)gridView:(KKGridView *)gridView didSelectItemAtIndexPath:(KKIndexPath *)indexPath;
  54
+- (void)gridView:(KKGridView *)gridView didDeselectItemAtIndexPath:(KKIndexPath *)indexPath;
  55
+- (KKIndexPath *)gridView:(KKGridView *)gridView willSelectItemAtIndexPath:(KKIndexPath *)indexPath;
  56
+- (KKIndexPath *)gridView:(KKGridView *)gridView willDeselectItemAtIndexPath:(KKIndexPath *)indexPath;
  57
+- (void)gridView:(KKGridView *)gridView willDisplayCell:(KKGridViewCell *)cell atIndexPath:(KKIndexPath *)indexPath;
  58
+@end
34 59
 
35 60
 @interface KKGridView : UIScrollView
36 61
 
@@ -47,7 +72,7 @@ typedef enum {
47 72
 
48 73
 #pragma mark - Data Source and Delegate
49 74
 @property (nonatomic, kk_weak) IBOutlet id <KKGridViewDataSource> dataSource;
50  
-@property (nonatomic, kk_weak) IBOutlet id <KKGridViewDelegate> gridDelegate;
  75
+@property (nonatomic, kk_weak) IBOutlet id <KKGridViewDelegate> delegate;
51 76
 
52 77
 #pragma mark - Getters
53 78
 
@@ -90,30 +115,3 @@ typedef enum {
90 115
 - (NSArray *)indexPathsForSelectedCells;
91 116
 
92 117
 @end
93  
-
94  
-
95  
-@protocol KKGridViewDataSource <NSObject>
96  
-@required
97  
-- (NSUInteger)gridView:(KKGridView *)gridView numberOfItemsInSection:(NSUInteger)section;
98  
-- (KKGridViewCell *)gridView:(KKGridView *)gridView cellForItemAtIndexPath:(KKIndexPath *)indexPath;
99  
-@optional
100  
-- (NSUInteger)numberOfSectionsInGridView:(KKGridView *)gridView;
101  
-- (NSString *)gridView:(KKGridView *)gridView titleForHeaderInSection:(NSUInteger)section;
102  
-- (NSString *)gridView:(KKGridView *)gridView titleForFooterInSection:(NSUInteger)section;
103  
-- (CGFloat)gridView:(KKGridView *)gridView heightForHeaderInSection:(NSUInteger)section;
104  
-- (CGFloat)gridView:(KKGridView *)gridView heightForFooterInSection:(NSUInteger)section;
105  
-- (UIView *)gridView:(KKGridView *)gridView viewForHeaderInSection:(NSUInteger)section;
106  
-- (UIView *)gridView:(KKGridView *)gridView viewForFooterInSection:(NSUInteger)section;
107  
-- (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
108  
-- (NSArray *)sectionIndexTitlesForGridView:(KKGridView *)gridView;
109  
-- (NSInteger)gridView:(KKGridView *)gridView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index;
110  
-@end
111  
-
112  
-@protocol KKGridViewDelegate <NSObject>
113  
-@optional
114  
-- (void)gridView:(KKGridView *)gridView didSelectItemAtIndexPath:(KKIndexPath *)indexPath;
115  
-- (void)gridView:(KKGridView *)gridView didDeselectItemAtIndexPath:(KKIndexPath *)indexPath;
116  
-- (KKIndexPath *)gridView:(KKGridView *)gridView willSelectItemAtIndexPath:(KKIndexPath *)indexPath;
117  
-- (KKIndexPath *)gridView:(KKGridView *)gridView willDeselectItemAtIndexPath:(KKIndexPath *)indexPath;
118  
-- (void)gridView:(KKGridView *)gridView willDisplayCell:(KKGridViewCell *)cell atIndexPath:(KKIndexPath *)indexPath;
119  
-@end
24  KKGridView/KKGridView.m
@@ -22,7 +22,7 @@
22 22
     NSUInteger itemCount;
23 23
 };
24 24
 
25  
-@interface KKGridView () <UIGestureRecognizerDelegate,UIScrollViewDelegate> {
  25
+@interface KKGridView () <UIGestureRecognizerDelegate> {
26 26
     // View-wrapper containers
27 27
     NSMutableArray *_footerViews;
28 28
     NSMutableArray *_rowViews;
@@ -135,7 +135,7 @@ + (void)animateIf:(BOOL)animated delay:(NSTimeInterval)delay options:(UIViewAnim
135 135
 @implementation KKGridView
136 136
 
137 137
 @synthesize dataSource = _dataSource;
138  
-@synthesize gridDelegate = _gridDelegate;
  138
+@dynamic delegate;
139 139
 @synthesize allowsMultipleSelection = _allowsMultipleSelection;
140 140
 @synthesize cellPadding = _cellPadding;
141 141
 @synthesize cellSize = _cellSize;
@@ -242,12 +242,12 @@ - (void)setDataSource:(id<KKGridViewDataSource>)dataSource
242 242
     }
243 243
 }
244 244
 
245  
-- (void)setGridDelegate:(id<KKGridViewDelegate>)gridDelegate
  245
+- (void)setDelegate:(id<KKGridViewDelegate>)delegate
246 246
 {
247  
-    if (gridDelegate != _gridDelegate)
  247
+    if (delegate != self.delegate)
248 248
     {
249  
-        _gridDelegate = gridDelegate;
250  
-#define RESPONDS_TO(sel) [_gridDelegate respondsToSelector:@selector(sel)]
  249
+        [super setDelegate:delegate];
  250
+#define RESPONDS_TO(sel) [self.delegate respondsToSelector:@selector(sel)]
251 251
         _delegateRespondsTo.didSelectItem    = RESPONDS_TO(gridView:didSelectItemAtIndexPath:);
252 252
         _delegateRespondsTo.willSelectItem   = RESPONDS_TO(gridView:willSelectItemAtIndexPath:);
253 253
         _delegateRespondsTo.didDeselectItem  = RESPONDS_TO(gridView:didDeselectItemAtIndexPath:);
@@ -678,7 +678,7 @@ - (CGFloat)_sectionHeightsCombinedUpToRow:(NSUInteger)row inSection:(NSUInteger)
678 678
 - (void)_displayCell:(KKGridViewCell *)cell atIndexPath:(KKIndexPath *)indexPath withAnimation:(KKGridViewAnimation)animation
679 679
 {
680 680
     if (_delegateRespondsTo.willDisplayCell) {
681  
-        [_gridDelegate gridView:self willDisplayCell:cell atIndexPath:indexPath];
  681
+        [self.delegate gridView:self willDisplayCell:cell atIndexPath:indexPath];
682 682
     }
683 683
     
684 684
     if ([_updateStack hasUpdateForIndexPath:indexPath]) {
@@ -1410,7 +1410,7 @@ - (void)_selectItemAtIndexPath:(KKIndexPath *)indexPath
1410 1410
     }
1411 1411
     
1412 1412
     if (_delegateRespondsTo.didSelectItem) {
1413  
-        [_gridDelegate gridView:self didSelectItemAtIndexPath:indexPath];
  1413
+        [self.delegate gridView:self didSelectItemAtIndexPath:indexPath];
1414 1414
     }
1415 1415
 }
1416 1416
 
@@ -1424,7 +1424,7 @@ - (void)_deselectAll
1424 1424
         
1425 1425
         if(_delegateRespondsTo.willDeselectItem)
1426 1426
         {
1427  
-            [_gridDelegate gridView:self willDeselectItemAtIndexPath:indexPath];
  1427
+            [self.delegate gridView:self willDeselectItemAtIndexPath:indexPath];
1428 1428
         }
1429 1429
     }
1430 1430
     
@@ -1434,7 +1434,7 @@ - (void)_deselectAll
1434 1434
 - (void)_deselectItemAtIndexPath:(KKIndexPath *)indexPath
1435 1435
 {
1436 1436
     if (_selectedIndexPaths.count > 0 && _delegateRespondsTo.willDeselectItem && indexPath.index != NSNotFound && indexPath.section != NSNotFound) {
1437  
-        KKIndexPath *redirectedPath = [_gridDelegate gridView:self willDeselectItemAtIndexPath:indexPath];
  1437
+        KKIndexPath *redirectedPath = [self.delegate gridView:self willDeselectItemAtIndexPath:indexPath];
1438 1438
         if (redirectedPath != nil && ![redirectedPath isEqual:indexPath]) {
1439 1439
             indexPath = redirectedPath;
1440 1440
         }
@@ -1447,7 +1447,7 @@ - (void)_deselectItemAtIndexPath:(KKIndexPath *)indexPath
1447 1447
     }
1448 1448
     
1449 1449
     if (_delegateRespondsTo.didDeselectItem) {
1450  
-        [_gridDelegate gridView:self didDeselectItemAtIndexPath:indexPath];
  1450
+        [self.delegate gridView:self didDeselectItemAtIndexPath:indexPath];
1451 1451
     }
1452 1452
 }
1453 1453
 
@@ -1487,7 +1487,7 @@ - (void)_handleSelection:(UILongPressGestureRecognizer *)recognizer
1487 1487
     KKIndexPath *indexPath = [self indexPathForItemAtPoint:locationInSelf];
1488 1488
     
1489 1489
     if (state == UIGestureRecognizerStateEnded && _delegateRespondsTo.willSelectItem)
1490  
-        indexPath = [_gridDelegate gridView:self willSelectItemAtIndexPath:indexPath];
  1490
+        indexPath = [self.delegate gridView:self willSelectItemAtIndexPath:indexPath];
1491 1491
     
1492 1492
     if (!indexPath || indexPath.index == NSNotFound || indexPath.section == NSNotFound) {
1493 1493
         [self _cancelHighlighting];
2  KKGridView/KKGridViewController.m
@@ -19,7 +19,7 @@ - (void)loadView
19 19
     _gridView = [[KKGridView alloc] initWithFrame:self.view.bounds];
20 20
     _gridView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
21 21
     _gridView.dataSource = self;
22  
-    _gridView.gridDelegate = self;
  22
+    _gridView.delegate = self;
23 23
     self.view = _gridView;
24 24
 }
25 25
 

5 notes on commit d254d3c

Zach Drayer
zadr commented on d254d3c April 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.

Jonathan Sterling
Collaborator
Simon Blommegård

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

Zach Drayer
zadr commented on d254d3c April 14, 2012

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

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