Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arc #9

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions PSCollectionView.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@

#pragma mark - Public Properties

@property (nonatomic, retain) UIView *headerView;
@property (nonatomic, retain) UIView *footerView;
@property (nonatomic, retain) UIView *emptyView;
@property (nonatomic, retain) UIView *loadingView;
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UIView *footerView;
@property (nonatomic, strong) UIView *emptyView;
@property (nonatomic, strong) UIView *loadingView;

@property (nonatomic, assign, readonly) CGFloat colWidth;
@property (nonatomic, assign, readonly) NSInteger numCols;
@property (nonatomic, assign) NSInteger numColsLandscape;
@property (nonatomic, assign) NSInteger numColsPortrait;
@property (nonatomic, assign) id <PSCollectionViewDelegate> collectionViewDelegate;
@property (nonatomic, assign) id <PSCollectionViewDataSource> collectionViewDataSource;
@property (nonatomic, unsafe_unretained) id <PSCollectionViewDelegate> collectionViewDelegate;
@property (nonatomic, unsafe_unretained) id <PSCollectionViewDataSource> collectionViewDataSource;

#pragma mark - Public Methods

Expand All @@ -55,7 +55,7 @@
Dequeues a reusable view that was previously initialized
This is similar to UITableView dequeueReusableCellWithIdentifier
*/
- (UIView *)dequeueReusableView;
- (UIView *)dequeueReusableView:(NSString *)identifier;

@end

Expand Down
79 changes: 43 additions & 36 deletions PSCollectionView.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#import "PSCollectionView.h"
#import "PSCollectionViewCell.h"

#define kMargin 8.0
#define kMargin 12.0

static inline NSString * PSCollectionKeyForIndex(NSInteger index) {
return [NSString stringWithFormat:@"%d", index];
Expand All @@ -38,12 +38,12 @@ static inline NSInteger PSCollectionIndexForKey(NSString *key) {

@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;
@property(nonatomic, assign) CGFloat left;
@property(nonatomic, assign) CGFloat top;
@property(nonatomic, assign, readonly) CGFloat right;
@property(nonatomic, assign, readonly) CGFloat bottom;
@property(nonatomic, assign) CGFloat width;
@property(nonatomic, assign) CGFloat height;

@end

Expand Down Expand Up @@ -115,10 +115,10 @@ @interface PSCollectionView () <UIGestureRecognizerDelegate>
@property (nonatomic, assign, readwrite) NSInteger numCols;
@property (nonatomic, assign) UIInterfaceOrientation orientation;

@property (nonatomic, retain) NSMutableSet *reuseableViews;
@property (nonatomic, retain) NSMutableDictionary *visibleViews;
@property (nonatomic, retain) NSMutableArray *viewKeysToRemove;
@property (nonatomic, retain) NSMutableDictionary *indexToRectMap;
@property (nonatomic, strong) NSMutableDictionary *reuseableViews;
@property (nonatomic, strong) NSMutableDictionary *visibleViews;
@property (nonatomic, strong) NSMutableArray *viewKeysToRemove;
@property (nonatomic, strong) NSMutableDictionary *indexToRectMap;


/**
Expand Down Expand Up @@ -178,31 +178,34 @@ - (id)initWithFrame:(CGRect)frame {
self.numColsLandscape = 0;
self.orientation = [UIApplication sharedApplication].statusBarOrientation;

self.reuseableViews = [NSMutableSet set];
self.reuseableViews = [NSMutableDictionary dictionary];
self.visibleViews = [NSMutableDictionary dictionary];
self.viewKeysToRemove = [NSMutableArray array];
self.indexToRectMap = [NSMutableDictionary dictionary];
}
return self;
}

- (void)awakeFromNib {
self.alwaysBounceVertical = YES;

self.colWidth = 0.0;
self.numCols = 0;
self.numColsPortrait = 0;
self.numColsLandscape = 0;
self.orientation = [UIApplication sharedApplication].statusBarOrientation;

self.reuseableViews = [NSMutableDictionary dictionary];
self.visibleViews = [NSMutableDictionary dictionary];
self.viewKeysToRemove = [NSMutableArray array];
self.indexToRectMap = [NSMutableDictionary dictionary];
}

- (void)dealloc {
// clear delegates
self.delegate = nil;
self.collectionViewDataSource = nil;
self.collectionViewDelegate = nil;

// release retains
self.headerView = nil;
self.footerView = nil;
self.emptyView = nil;
self.loadingView = nil;

self.reuseableViews = nil;
self.visibleViews = nil;
self.viewKeysToRemove = nil;
self.indexToRectMap = nil;
[super dealloc];
}

#pragma mark - Setters
Expand All @@ -211,8 +214,7 @@ - (void)setLoadingView:(UIView *)loadingView {
if (_loadingView && [_loadingView respondsToSelector:@selector(removeFromSuperview)]) {
[_loadingView removeFromSuperview];
}
[_loadingView release], _loadingView = nil;
_loadingView = [loadingView retain];
_loadingView = loadingView;

[self addSubview:_loadingView];
}
Expand Down Expand Up @@ -262,11 +264,9 @@ - (void)relayoutViews {

// Add headerView if it exists
if (self.headerView) {
self.headerView.top = kMargin;
top = self.headerView.top;
[self addSubview:self.headerView];
top += self.headerView.height;
top += kMargin;
}

if (numViews > 0) {
Expand Down Expand Up @@ -336,7 +336,6 @@ - (void)relayoutViews {
self.footerView.top = totalHeight;
[self addSubview:self.footerView];
totalHeight += self.footerView.height;
totalHeight += kMargin;
}

self.contentSize = CGSizeMake(self.width, totalHeight);
Expand Down Expand Up @@ -404,7 +403,7 @@ - (void)removeAndAddCellsIfNecessary {

// Setup gesture recognizer
if ([newView.gestureRecognizers count] == 0) {
PSCollectionViewTapGestureRecognizer *gr = [[[PSCollectionViewTapGestureRecognizer alloc] initWithTarget:self action:@selector(didSelectView:)] autorelease];
PSCollectionViewTapGestureRecognizer *gr = [[PSCollectionViewTapGestureRecognizer alloc] initWithTarget:self action:@selector(didSelectView:)];
gr.delegate = self;
[newView addGestureRecognizer:gr];
newView.userInteractionEnabled = YES;
Expand All @@ -417,13 +416,16 @@ - (void)removeAndAddCellsIfNecessary {

#pragma mark - Reusing Views

- (PSCollectionViewCell *)dequeueReusableView {
PSCollectionViewCell *view = [self.reuseableViews anyObject];
- (PSCollectionViewCell *)dequeueReusableView:(NSString *)identifier {
NSMutableSet *set = [self.reuseableViews objectForKey:identifier];
if (set == nil) {
set = [NSMutableSet set];
[self.reuseableViews setValue:set forKey:identifier];
}
PSCollectionViewCell *view = [set anyObject];
if (view) {
// Found a reusable view, remove it from the set
[view retain];
[self.reuseableViews removeObject:view];
[view autorelease];
[set removeObject:view];
}

return view;
Expand All @@ -434,7 +436,12 @@ - (void)enqueueReusableView:(PSCollectionViewCell *)view {
[view performSelector:@selector(prepareForReuse)];
}
view.frame = CGRectZero;
[self.reuseableViews addObject:view];
NSMutableSet *set = [self.reuseableViews objectForKey:view.identifier];
if (set != nil) {
set = [NSMutableSet set];
[self.reuseableViews setValue:set forKey:view.identifier];
}
[set addObject:view];
[view removeFromSuperview];
}

Expand Down
3 changes: 2 additions & 1 deletion PSCollectionViewCell.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@

@interface PSCollectionViewCell : UIView

@property (nonatomic, retain) id object;
@property (nonatomic, strong) NSString *identifier;//to reuse the cell, set this field
@property (nonatomic, strong) id object;

- (void)prepareForReuse;
- (void)fillViewWithObject:(id)object;
Expand Down
8 changes: 2 additions & 6 deletions PSCollectionViewCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ @interface PSCollectionViewCell ()

@implementation PSCollectionViewCell

@synthesize
object = _object;
@synthesize object = _object;
@synthesize identifier = _identifier;

- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
Expand All @@ -39,10 +39,6 @@ - (id)initWithFrame:(CGRect)frame {
return self;
}

- (void)dealloc {
self.object = nil;
[super dealloc];
}

- (void)prepareForReuse {
}
Expand Down