Skip to content
This repository has been archived by the owner on Feb 21, 2023. It is now read-only.

Commit

Permalink
changed Methods-Ordering and fixed Cell-Reusing in reloadData
Browse files Browse the repository at this point in the history
  • Loading branch information
messi committed Apr 19, 2012
1 parent 49cd9c9 commit a462087
Showing 1 changed file with 60 additions and 52 deletions.
112 changes: 60 additions & 52 deletions NGVaryingGridView/NGVaryingGridView/NGVaryingGridView.m
Expand Up @@ -10,10 +10,10 @@

@interface NGVaryingGridView () <UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSArray *gridRects;
@property (nonatomic, strong) NSMutableDictionary *gridCells;
@property (nonatomic, strong) NSMutableSet *reuseableCells;
@property (nonatomic, strong) NSArray *gridRects;
@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) UIView *stickyViewForTopPosition;
@property (nonatomic, strong) UIView *stickyViewForLeftPosition;
Expand All @@ -29,14 +29,16 @@ - (void)bringScrollingIndicatorsToFront;

@implementation NGVaryingGridView

@synthesize scrollView = _scrollView;
@synthesize scrollViewDelegate = _scrollViewDelegate;
@synthesize gridViewDelegate = _gridViewDelegate;
@synthesize scrollView = _scrollView;
@synthesize gridRects = _gridRects;
@synthesize gridCells = _gridCells;
@synthesize reuseableCells = _reuseableCells;

@synthesize maximumContentWidth = _maximumContentWidth;
@synthesize maximumContentHeight = _maximumContentHeight;
@synthesize gridRects = _gridRects;
@synthesize reuseableCells = _reuseableCells;

@synthesize stickyViewForTopPosition = _stickyViewForTopPosition;
@synthesize stickyViewForLeftPosition = _stickyViewForLeftPosition;
@synthesize stickyViewForLeftPositionPadding = _stickyViewForLeftPositionPadding;
Expand All @@ -58,7 +60,6 @@ - (id)initWithFrame:(CGRect)frame
_maximumContentHeight = CGFLOAT_MAX;
_maximumContentWidth = CGFLOAT_MAX;


[super addSubview:_scrollView];
_scrollView.delegate = self;
}
Expand Down Expand Up @@ -105,6 +106,7 @@ - (void)reloadData {

self.gridRects = [self.gridViewDelegate rectsForCellsInGridView:self];
[self.gridCells.allValues makeObjectsPerformSelector:@selector(removeFromSuperview)];
[self.reuseableCells addObjectsFromArray:self.gridCells.allValues];
[self.gridCells removeAllObjects];

for (NSValue *rectValue in self.gridRects) {
Expand All @@ -130,64 +132,20 @@ - (UIView *)gridCellWithCGPoint:(CGPoint)point {
return nil;
}

- (void)scrollToGridCell:(UIView *)cell animated:(BOOL)animated {
[self.scrollView scrollRectToVisible:cell.frame animated:animated];
}

- (CGRect)visibleRect {
CGRect visibleRect;
visibleRect.origin = self.scrollView.contentOffset;
visibleRect.size = self.scrollView.bounds.size;

float scale = 1.0 / self.scrollView.zoomScale;
visibleRect.origin.x *= scale;
visibleRect.origin.y *= scale;
visibleRect.size.width *= scale;
visibleRect.size.height *= scale;

return visibleRect;
}

- (void)addOverlayView:(UIView *)overlayView {
[super addSubview:overlayView];
}

- (void)addSubview:(UIView *)view {
[self.scrollView addSubview:view];
}

- (BOOL)isDirectionalLockEnabled {
return self.scrollView.isDirectionalLockEnabled;
}

- (void)setDirectionalLockEnabled:(BOOL)directionalLockEnabled {
self.scrollView.directionalLockEnabled = directionalLockEnabled;
}

- (void)setShowsVerticalScrollIndicator:(BOOL)showsVerticalScrollIndicator {
self.scrollView.showsVerticalScrollIndicator = showsVerticalScrollIndicator;
}

- (BOOL)showsVerticalScrollIndicator {
return self.scrollView.showsVerticalScrollIndicator;
}

- (void)setShowsHorizontalScrollIndicator:(BOOL)showsHorizontalScrollIndicator {
self.scrollView.showsHorizontalScrollIndicator = showsHorizontalScrollIndicator;
}

- (BOOL)showsHorizontalScrollIndicator {
return self.scrollView.showsHorizontalScrollIndicator;
}

- (void)setStickyView:(UIView *)view lockPosition:(NGVaryingGridViewLockPosition)lockPosition {
switch (lockPosition) {
case NGVaryingGridViewLockPositionTop:
[self.stickyViewForTopPosition removeFromSuperview];
self.stickyViewForTopPositionPadding = view.frame.origin.y;
self.stickyViewForTopPosition = view;
break;

case NGVaryingGridViewLockPositionLeft:
[self.stickyViewForLeftPosition removeFromSuperview];
self.stickyViewForLeftPositionPadding = view.frame.origin.x;
self.stickyViewForLeftPosition = view;
break;
Expand Down Expand Up @@ -268,6 +226,56 @@ - (void)bringScrollingIndicatorsToFront {
}
}

////////////////////////////////////////////////////////////////////////
#pragma mark - UIScrolLView Forwarding
////////////////////////////////////////////////////////////////////////

- (void)scrollToGridCell:(UIView *)cell animated:(BOOL)animated {
[self.scrollView scrollRectToVisible:cell.frame animated:animated];
}

- (CGRect)visibleRect {
CGRect visibleRect;
visibleRect.origin = self.scrollView.contentOffset;
visibleRect.size = self.scrollView.bounds.size;

float scale = 1.0 / self.scrollView.zoomScale;
visibleRect.origin.x *= scale;
visibleRect.origin.y *= scale;
visibleRect.size.width *= scale;
visibleRect.size.height *= scale;

return visibleRect;
}

- (void)addSubview:(UIView *)view {
[self.scrollView addSubview:view];
}

- (BOOL)isDirectionalLockEnabled {
return self.scrollView.isDirectionalLockEnabled;
}

- (void)setDirectionalLockEnabled:(BOOL)directionalLockEnabled {
self.scrollView.directionalLockEnabled = directionalLockEnabled;
}

- (void)setShowsVerticalScrollIndicator:(BOOL)showsVerticalScrollIndicator {
self.scrollView.showsVerticalScrollIndicator = showsVerticalScrollIndicator;
}

- (BOOL)showsVerticalScrollIndicator {
return self.scrollView.showsVerticalScrollIndicator;
}

- (void)setShowsHorizontalScrollIndicator:(BOOL)showsHorizontalScrollIndicator {
self.scrollView.showsHorizontalScrollIndicator = showsHorizontalScrollIndicator;
}

- (BOOL)showsHorizontalScrollIndicator {
return self.scrollView.showsHorizontalScrollIndicator;
}

////////////////////////////////////////////////////////////////////////
#pragma mark - UIScrollViewDelegate
////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit a462087

Please sign in to comment.