Skip to content

Commit

Permalink
fix missing category
Browse files Browse the repository at this point in the history
  • Loading branch information
ptshih committed Apr 24, 2012
1 parent 749794a commit 8536a50
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 10 deletions.
6 changes: 4 additions & 2 deletions PSCollectionView.h
Expand Up @@ -23,6 +23,8 @@

#import <UIKit/UIKit.h>

@class PSCollectionViewCell;

@protocol PSCollectionViewDelegate, PSCollectionViewDataSource;

@interface PSCollectionView : UIScrollView
Expand Down Expand Up @@ -62,7 +64,7 @@
@protocol PSCollectionViewDelegate <NSObject>

@optional
- (void)collectionView:(PSCollectionView *)collectionView didSelectView:(UIView *)view atIndex:(NSInteger)index;
- (void)collectionView:(PSCollectionView *)collectionView didSelectView:(PSCollectionViewCell *)view atIndex:(NSInteger)index;

@end

Expand All @@ -72,7 +74,7 @@

@required
- (NSInteger)numberOfViewsInCollectionView:(PSCollectionView *)collectionView;
- (UIView *)collectionView:(PSCollectionView *)collectionView viewAtIndex:(NSInteger)index;
- (PSCollectionViewCell *)collectionView:(PSCollectionView *)collectionView viewAtIndex:(NSInteger)index;
- (CGFloat)heightForViewAtIndex:(NSInteger)index;

@end
83 changes: 75 additions & 8 deletions PSCollectionView.m
Expand Up @@ -22,7 +22,7 @@
// THE SOFTWARE.

#import "PSCollectionView.h"
#import "UIView+PSKit.h"
#import "PSCollectionViewCell.h"

#define kMargin 8.0

Expand All @@ -34,6 +34,73 @@ static inline NSInteger PSCollectionIndexForKey(NSString *key) {
return [key integerValue];
}

#pragma mark - UIView Category

@interface UIView (PSCollectionView)

@property(nonatomic) CGFloat left;
@property(nonatomic) CGFloat top;
@property(nonatomic, readonly) CGFloat right;
@property(nonatomic, readonly) CGFloat bottom;
@property(nonatomic) CGFloat width;
@property(nonatomic) CGFloat height;

@end

@implementation UIView (PSCollectionView)

- (CGFloat)left {
return self.frame.origin.x;
}

- (void)setLeft:(CGFloat)x {
CGRect frame = self.frame;
frame.origin.x = x;
self.frame = frame;
}

- (CGFloat)top {
return self.frame.origin.y;
}

- (void)setTop:(CGFloat)y {
CGRect frame = self.frame;
frame.origin.y = y;
self.frame = frame;
}

- (CGFloat)right {
return self.frame.origin.x + self.frame.size.width;
}

- (CGFloat)bottom {
return self.frame.origin.y + self.frame.size.height;
}

- (CGFloat)width {
return self.frame.size.width;
}

- (void)setWidth:(CGFloat)width {
CGRect frame = self.frame;
frame.size.width = width;
self.frame = frame;
}

- (CGFloat)height {
return self.frame.size.height;
}

- (void)setHeight:(CGFloat)height {
CGRect frame = self.frame;
frame.size.height = height;
self.frame = frame;
}

@end

#pragma mark - Gesture Recognizer

// This is just so we know that we sent this tap gesture recognizer in the delegate
@interface PSCollectionViewTapGestureRecognizer : UITapGestureRecognizer
@end
Expand Down Expand Up @@ -63,7 +130,7 @@ - (void)relayoutViews;
Stores a view for later reuse
TODO: add an identifier like UITableView
*/
- (void)enqueueReusableView:(UIView *)view;
- (void)enqueueReusableView:(PSCollectionViewCell *)view;

/**
Magic!
Expand Down Expand Up @@ -175,7 +242,7 @@ - (void)relayoutViews {

// Reset all state
[self.visibleViews enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
UIView *view = (UIView *)obj;
PSCollectionViewCell *view = (PSCollectionViewCell *)obj;
[self enqueueReusableView:view];
}];
[self.visibleViews removeAllObjects];
Expand Down Expand Up @@ -291,7 +358,7 @@ - (void)removeAndAddCellsIfNecessary {

// Remove all rows that are not inside the visible rect
[self.visibleViews enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
UIView *view = (UIView *)obj;
PSCollectionViewCell *view = (PSCollectionViewCell *)obj;
CGRect viewRect = view.frame;
if (!CGRectIntersectsRect(visibleRect, viewRect)) {
[self enqueueReusableView:view];
Expand Down Expand Up @@ -331,7 +398,7 @@ - (void)removeAndAddCellsIfNecessary {
// If view is within visible rect and is not already shown
if (![self.visibleViews objectForKey:key] && CGRectIntersectsRect(visibleRect, rect)) {
// Only add views if not visible
UIView *newView = [self.collectionViewDataSource collectionView:self viewAtIndex:i];
PSCollectionViewCell *newView = [self.collectionViewDataSource collectionView:self viewAtIndex:i];
newView.frame = CGRectFromString([self.indexToRectMap objectForKey:key]);
[self addSubview:newView];

Expand All @@ -350,8 +417,8 @@ - (void)removeAndAddCellsIfNecessary {

#pragma mark - Reusing Views

- (UIView *)dequeueReusableView {
UIView *view = [self.reuseableViews anyObject];
- (PSCollectionViewCell *)dequeueReusableView {
PSCollectionViewCell *view = [self.reuseableViews anyObject];
if (view) {
// Found a reusable view, remove it from the set
[view retain];
Expand All @@ -362,7 +429,7 @@ - (UIView *)dequeueReusableView {
return view;
}

- (void)enqueueReusableView:(UIView *)view {
- (void)enqueueReusableView:(PSCollectionViewCell *)view {
if ([view respondsToSelector:@selector(prepareForReuse)]) {
[view performSelector:@selector(prepareForReuse)];
}
Expand Down

0 comments on commit 8536a50

Please sign in to comment.