Permalink
Browse files

Modified the Grid View to not rely on being its own delegate. The pro…

…perty gridDelegate is now depricated.
  • Loading branch information...
1 parent e33f02f commit 37c92c00352ea0579e3fda3dd590fd6292472cb5 @danielctull committed Feb 12, 2010
View
2 Classes/DTGridViewExampleDataSourceAndDelegate.m
@@ -41,7 +41,7 @@ - (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Scroll" style:UIBarButtonItemStyleBordered target:self action:@selector(scroll)] autorelease];
self.title = @"DTGridView";
- self.gridView.gridDelegate = self;
+ self.gridView.delegate = self;
self.gridView.dataSource = self;
self.gridView.bounces = YES;
}
View
2 Classes/DTInfiniteGridViewExampleViewController.m
@@ -21,7 +21,7 @@ - (void)viewDidLoad {
gridView.dataSource = self;
gridView.infiniteVerticalScrolling = NO;
gridView.infiniteHorizontalScrolling = YES;
- gridView.gridDelegate = self;
+ gridView.delegate = self;
gridView.pagingEnabled = NO;
[self.view addSubview:gridView];
}
View
3 DTGridView/DTGridView.h
@@ -72,7 +72,6 @@ struct DTOutset {
*/
@interface DTGridView : UIScrollView <UIScrollViewDelegate, DTGridViewCellDelegate> {
- NSObject<DTGridViewDelegate> *gridDelegate;
NSObject<DTGridViewDataSource> *dataSource;
CGPoint cellOffset;
@@ -110,7 +109,7 @@ struct DTOutset {
@abstract The object that acts as the delegate of the receiving grid view.
@discussion The delegate must adopt the DTGridViewDelegate protocol. The delegate is not retained.
*/
-@property (nonatomic, assign) IBOutlet NSObject<DTGridViewDelegate> *gridDelegate;
+@property (nonatomic, assign) IBOutlet id<DTGridViewDelegate> delegate;
/*!
@abstract The offset for each cell with respect to the cells above and to the right.
View
120 DTGridView/DTGridView.m
@@ -36,7 +36,8 @@ - (void)checkRow:(NSInteger)row column:(NSInteger)col goingLeft:(BOOL)goingLeft;
@implementation DTGridView
-@synthesize dataSource, gridCells, numberOfRows, cellOffset, gridDelegate, outset;
+@dynamic delegate;
+@synthesize dataSource, gridCells, numberOfRows, cellOffset, outset;
NSInteger intSort(id info1, id info2, void *context) {
@@ -55,10 +56,6 @@ NSInteger intSort(id info1, id info2, void *context) {
return NSOrderedSame;
}
-- (id)delegate {
- return self;
-}
-
- (void)reloadData {
[self loadData];
[self setNeedsDisplay];
@@ -74,8 +71,6 @@ - (void)drawRect:(CGRect)rect {
rowHeights = [[NSMutableArray alloc] init];
cellsOnScreen = [[NSMutableArray alloc] init];
- super.delegate = self;
-
freeCells = [[NSMutableArray alloc] init];
cellInfoForCellsOnScreen = [[NSMutableArray alloc] init];
@@ -95,13 +90,14 @@ - (void)drawRect:(CGRect)rect {
}
- (void)didLoad {
- if ([self.gridDelegate respondsToSelector:@selector(gridViewDidLoad:)])
- [self.gridDelegate gridViewDidLoad:self];
+ if ([self.delegate respondsToSelector:@selector(gridViewDidLoad:)])
+ [self.delegate gridViewDidLoad:self];
}
- (void)layoutSubviews {
[super layoutSubviews];
[self checkViews];
+ [self fireEdgeScroll];
}
- (void)dealloc {
@@ -589,8 +585,8 @@ - (void)scrollViewToRow:(NSInteger)rowIndex column:(NSInteger)columnIndex scroll
if (!animated)
[self checkViews];
- if ([self.gridDelegate respondsToSelector:@selector(gridView:didProgrammaticallyScrollToRow:column:)])
- [self.gridDelegate gridView:self didProgrammaticallyScrollToRow:rowIndex column:columnIndex];
+ if ([self.delegate respondsToSelector:@selector(gridView:didProgrammaticallyScrollToRow:column:)])
+ [self.delegate gridView:self didProgrammaticallyScrollToRow:rowIndex column:columnIndex];
}
@@ -612,120 +608,36 @@ - (void)selectRow:(NSInteger)rowIndex column:(NSInteger)columnIndex scrollPositi
[self scrollViewToRow:rowIndex column:columnIndex scrollPosition:position animated:animated];
}
-#pragma mark UIScrollViewDelegate methods
-
-//All scrollview delegate methods are implemented to pass them through to the gridview's delegate.
-
-- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
- if ([self.gridDelegate respondsToSelector:@selector(scrollViewDidEndDecelerating:)])
- [self.gridDelegate scrollViewDidEndDecelerating:scrollView];
-
- [self positionCheck];
- [self fireEdgeScroll];
-}
-
-- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
- if ([self.gridDelegate respondsToSelector:@selector(scrollViewDidEndDragging:willDecelerate:)])
- [self.gridDelegate scrollViewDidEndDragging:scrollView willDecelerate:decelerate];
-
- if (!decelerate)
- [self positionCheck];
-
- [self fireEdgeScroll];
-}
-
-- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
- if ([self.gridDelegate respondsToSelector:@selector(scrollViewDidEndScrollingAnimation:)])
- [self.gridDelegate scrollViewDidEndScrollingAnimation:scrollView];
-
- //[self positionCheck];
-
- //[self fireEdgeScroll];
-}
-
-- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
- if ([self.gridDelegate respondsToSelector:@selector(scrollViewDidEndZooming:withView:atScale:)])
- [self.gridDelegate scrollViewDidEndZooming:scrollView withView:view atScale:scale];
-}
-
-- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
-
- //[self checkViews];
-
- // TODO: Optimize the edge scrolling check
-
- [self positionCheck];
-
- [self fireEdgeScroll];
-
- if ([self.gridDelegate respondsToSelector:@selector(scrollViewDidScroll:)])
- [self.gridDelegate scrollViewDidScroll:scrollView];
-
-}
-
-- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView {
- if ([self.gridDelegate respondsToSelector:@selector(scrollViewDidScrollToTop:)])
- [self.gridDelegate scrollViewDidScrollToTop:scrollView];
-}
-
-- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
- if ([self.gridDelegate respondsToSelector:@selector(scrollViewWillBeginDecelerating:)])
- [self.gridDelegate scrollViewWillBeginDecelerating:scrollView];
-}
-
-- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
- if ([self.gridDelegate respondsToSelector:@selector(scrollViewWillBeginDragging:)])
- [self.gridDelegate scrollViewWillBeginDragging:scrollView];
-
- //if ([self respondsToSelector:@selector(positionCheck)])
- //[self positionCheck];
- //[self fireEdgeScroll];
-}
-
-- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView {
- if ([self.gridDelegate respondsToSelector:@selector(scrollViewShouldScrollToTop:)])
- return [self.gridDelegate scrollViewShouldScrollToTop:scrollView];
-
- return YES;
-}
-
-- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
- if ([self.gridDelegate respondsToSelector:@selector(viewForZoomingInScrollView:)])
- return [self.gridDelegate viewForZoomingInScrollView:scrollView];
-
- return nil;
-}
-
- (void)positionCheck {}
- (void)fireEdgeScroll {
if (self.pagingEnabled)
- if ([self.gridDelegate respondsToSelector:@selector(pagedGridView:didScrollToRow:column:)])
- [self.gridDelegate pagedGridView:self didScrollToRow:((NSInteger)(self.contentOffset.y / self.frame.size.height)) column:((NSInteger)(self.contentOffset.x / self.frame.size.width))];
+ if ([self.delegate respondsToSelector:@selector(pagedGridView:didScrollToRow:column:)])
+ [self.delegate pagedGridView:self didScrollToRow:((NSInteger)(self.contentOffset.y / self.frame.size.height)) column:((NSInteger)(self.contentOffset.x / self.frame.size.width))];
- if ([self.gridDelegate respondsToSelector:@selector(gridView:scrolledToEdge:)]) {
+ if ([self.delegate respondsToSelector:@selector(gridView:scrolledToEdge:)]) {
if (self.contentOffset.x <= 0)
- [self.gridDelegate gridView:self scrolledToEdge:DTGridViewEdgeLeft];
+ [self.delegate gridView:self scrolledToEdge:DTGridViewEdgeLeft];
if (self.contentOffset.x >= self.contentSize.width - self.frame.size.width)
- [self.gridDelegate gridView:self scrolledToEdge:DTGridViewEdgeRight];
+ [self.delegate gridView:self scrolledToEdge:DTGridViewEdgeRight];
if (self.contentOffset.y <= 0)
- [self.gridDelegate gridView:self scrolledToEdge:DTGridViewEdgeTop];
+ [self.delegate gridView:self scrolledToEdge:DTGridViewEdgeTop];
if (self.contentOffset.y >= self.contentSize.height - self.frame.size.height)
- [self.gridDelegate gridView:self scrolledToEdge:DTGridViewEdgeBottom];
+ [self.delegate gridView:self scrolledToEdge:DTGridViewEdgeBottom];
}
}
- (void)gridViewCellWasTouched:(DTGridViewCell *)cell {
[self bringSubviewToFront:cell];
- if ([self.gridDelegate respondsToSelector:@selector(gridView:selectionMadeAtRow:column:)])
- [self.gridDelegate gridView:self selectionMadeAtRow:cell.yPosition column:cell.xPosition];
+ if ([self.delegate respondsToSelector:@selector(gridView:selectionMadeAtRow:column:)])
+ [self.delegate gridView:self selectionMadeAtRow:cell.yPosition column:cell.xPosition];
}
@end
View
2 DTSnapGridView/DTSnapGridView.h
@@ -15,7 +15,7 @@
@interface DTSnapGridView : DTGridView {
DTSnapGridViewCell *selectedCell;
}
-@property (nonatomic, assign) IBOutlet NSObject<DTSnapGridViewDelegate> *gridDelegate;
+@property (nonatomic, assign) IBOutlet id<DTSnapGridViewDelegate> delegate;
@end
View
44 DTSnapGridView/DTSnapGridView.m
@@ -11,17 +11,18 @@
@implementation DTSnapGridView
-- (NSObject<DTSnapGridViewDelegate> *)gridDelegate {
- return (NSObject<DTSnapGridViewDelegate> *)[super gridDelegate];
-}
+@dynamic delegate;
-- (void)setGridDelegate:(NSObject<DTSnapGridViewDelegate> *)aDelegate {
- super.gridDelegate = aDelegate;
+- (void)drawRect:(CGRect)rect {
+ NSLog(@"%@:%s", self, _cmd);
+ [super drawRect:rect];
}
- (void)didLoad {
for (DTSnapGridViewCell *v in self.subviews) {
+ NSLog(@"%@:%s %@", self, _cmd, v);
+
v.slideAmount = (2*(v.center.x-self.contentOffset.x) + 2*v.frame.size.width - self.frame.size.width)/(self.frame.size.width - v.frame.size.width);
if (v.slideAmount > 0.5 && v.slideAmount <= 1.5 && ![v isEqual:selectedCell]) {
@@ -31,21 +32,34 @@ - (void)didLoad {
[super didLoad];
}
-#pragma mark UIScrollViewDelegate Methods
-
-- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
-
- [super scrollViewDidScroll:scrollView];
-
+- (void)layoutSubviews {
+ NSLog(@"%@:%s", self, _cmd);
+ [super layoutSubviews];
+}/*
for (DTSnapGridViewCell *v in self.subviews) {
+ NSLog(@"%@:%s %@", self, _cmd, v);
+
v.slideAmount = (2*(v.center.x-self.contentOffset.x) + 2*v.frame.size.width - self.frame.size.width)/(self.frame.size.width - v.frame.size.width);
if (v.slideAmount > 0.5 && v.slideAmount <= 1.5 && ![v isEqual:selectedCell]) {
selectedCell = v;
}
}
+ [super layoutSubviews];
}
+/*
+ for (DTSnapGridViewCell *v in self.subviews) {
+
+ NSLog(@"%@:%s %@", self, _cmd, v);
+
+ v.slideAmount = (2*(v.center.x-self.contentOffset.x) + 2*v.frame.size.width - self.frame.size.width)/(self.frame.size.width - v.frame.size.width);
+
+ if (v.slideAmount > 0.5 && v.slideAmount <= 1.5 && ![v isEqual:selectedCell]) {
+ selectedCell = v;
+ }
+ }
+}*/
- (CGFloat)findWidthForRow:(NSInteger)row column:(NSInteger)column {
NSInteger w = (NSInteger)self.frame.size.width/3;
@@ -55,16 +69,16 @@ - (CGFloat)findWidthForRow:(NSInteger)row column:(NSInteger)column {
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
[super scrollViewDidEndDecelerating:scrollView];
[self scrollViewToRow:0 column:selectedCell.xPosition scrollPosition:DTGridViewScrollPositionMiddleCenter animated:YES];
- if ([self.gridDelegate respondsToSelector:@selector(snapGridView:didHighlightIndex:)])
- [self.gridDelegate snapGridView:self didHighlightIndex:selectedCell.xPosition];
+ if ([self.delegate respondsToSelector:@selector(snapGridView:didHighlightIndex:)])
+ [self.delegate snapGridView:self didHighlightIndex:selectedCell.xPosition];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
[super scrollViewDidEndDragging:scrollView willDecelerate:decelerate];
[self scrollViewToRow:0 column:selectedCell.xPosition scrollPosition:DTGridViewScrollPositionMiddleCenter animated:YES];
if (!decelerate)
- if ([self.gridDelegate respondsToSelector:@selector(snapGridView:didHighlightIndex:)])
- [self.gridDelegate snapGridView:self didHighlightIndex:selectedCell.xPosition];
+ if ([self.delegate respondsToSelector:@selector(snapGridView:didHighlightIndex:)])
+ [self.delegate snapGridView:self didHighlightIndex:selectedCell.xPosition];
}
@end

0 comments on commit 37c92c0

Please sign in to comment.