From ab1552b2d20b87dc14eb0c00108e8877fdebbfc7 Mon Sep 17 00:00:00 2001 From: Matt Galloway Date: Wed, 23 May 2012 10:25:27 +0100 Subject: [PATCH 1/4] ARC-ify --- PSCollectionView.h | 12 ++++++------ PSCollectionView.m | 27 ++++++--------------------- PSCollectionViewCell.h | 2 +- PSCollectionViewCell.m | 4 ---- 4 files changed, 13 insertions(+), 32 deletions(-) diff --git a/PSCollectionView.h b/PSCollectionView.h index 0bbe669..0b1a5fb 100644 --- a/PSCollectionView.h +++ b/PSCollectionView.h @@ -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 collectionViewDelegate; -@property (nonatomic, assign) id collectionViewDataSource; +@property (nonatomic, unsafe_unretained) id collectionViewDelegate; +@property (nonatomic, unsafe_unretained) id collectionViewDataSource; #pragma mark - Public Methods diff --git a/PSCollectionView.m b/PSCollectionView.m index 768c76c..689867a 100644 --- a/PSCollectionView.m +++ b/PSCollectionView.m @@ -115,10 +115,10 @@ @interface PSCollectionView () @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) NSMutableSet *reuseableViews; +@property (nonatomic, strong) NSMutableDictionary *visibleViews; +@property (nonatomic, strong) NSMutableArray *viewKeysToRemove; +@property (nonatomic, strong) NSMutableDictionary *indexToRectMap; /** @@ -191,18 +191,6 @@ - (void)dealloc { 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 @@ -211,8 +199,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]; } @@ -404,7 +391,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; @@ -421,9 +408,7 @@ - (PSCollectionViewCell *)dequeueReusableView { PSCollectionViewCell *view = [self.reuseableViews anyObject]; if (view) { // Found a reusable view, remove it from the set - [view retain]; [self.reuseableViews removeObject:view]; - [view autorelease]; } return view; diff --git a/PSCollectionViewCell.h b/PSCollectionViewCell.h index b37551b..c9ba811 100644 --- a/PSCollectionViewCell.h +++ b/PSCollectionViewCell.h @@ -25,7 +25,7 @@ @interface PSCollectionViewCell : UIView -@property (nonatomic, retain) id object; +@property (nonatomic, strong) id object; - (void)prepareForReuse; - (void)fillViewWithObject:(id)object; diff --git a/PSCollectionViewCell.m b/PSCollectionViewCell.m index 4ffb17f..ff047ec 100644 --- a/PSCollectionViewCell.m +++ b/PSCollectionViewCell.m @@ -39,10 +39,6 @@ - (id)initWithFrame:(CGRect)frame { return self; } -- (void)dealloc { - self.object = nil; - [super dealloc]; -} - (void)prepareForReuse { } From 19c00081e10a65d0453eccb430acb1ac4242aaef Mon Sep 17 00:00:00 2001 From: Peter Shih Date: Fri, 25 May 2012 00:06:38 -0400 Subject: [PATCH 2/4] fixing some margins --- PSCollectionView.m | 3 --- 1 file changed, 3 deletions(-) diff --git a/PSCollectionView.m b/PSCollectionView.m index 689867a..881e650 100644 --- a/PSCollectionView.m +++ b/PSCollectionView.m @@ -249,11 +249,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) { @@ -323,7 +321,6 @@ - (void)relayoutViews { self.footerView.top = totalHeight; [self addSubview:self.footerView]; totalHeight += self.footerView.height; - totalHeight += kMargin; } self.contentSize = CGSizeMake(self.width, totalHeight); From abad0fd09e3013b67bce4ad885dae3372a029429 Mon Sep 17 00:00:00 2001 From: Peter Shih Date: Fri, 25 May 2012 00:07:08 -0400 Subject: [PATCH 3/4] adding explicit assigns to primitive properties --- PSCollectionView.m | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/PSCollectionView.m b/PSCollectionView.m index 881e650..4519676 100644 --- a/PSCollectionView.m +++ b/PSCollectionView.m @@ -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 From 729cf5b4b44cea63a6485867f23365e3a7f27061 Mon Sep 17 00:00:00 2001 From: disorderdev Date: Sun, 9 Sep 2012 21:02:56 +0800 Subject: [PATCH 4/4] support multiple cell types use large margin(8.0 --> 12.0) --- PSCollectionView.h | 2 +- PSCollectionView.m | 39 ++++++++++++++++++++++++++++++++------- PSCollectionViewCell.h | 1 + PSCollectionViewCell.m | 4 ++-- 4 files changed, 36 insertions(+), 10 deletions(-) diff --git a/PSCollectionView.h b/PSCollectionView.h index 0b1a5fb..a569274 100644 --- a/PSCollectionView.h +++ b/PSCollectionView.h @@ -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 diff --git a/PSCollectionView.m b/PSCollectionView.m index 4519676..1bbb5f4 100644 --- a/PSCollectionView.m +++ b/PSCollectionView.m @@ -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]; @@ -115,7 +115,7 @@ @interface PSCollectionView () @property (nonatomic, assign, readwrite) NSInteger numCols; @property (nonatomic, assign) UIInterfaceOrientation orientation; -@property (nonatomic, strong) NSMutableSet *reuseableViews; +@property (nonatomic, strong) NSMutableDictionary *reuseableViews; @property (nonatomic, strong) NSMutableDictionary *visibleViews; @property (nonatomic, strong) NSMutableArray *viewKeysToRemove; @property (nonatomic, strong) NSMutableDictionary *indexToRectMap; @@ -178,7 +178,7 @@ - (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]; @@ -186,6 +186,21 @@ - (id)initWithFrame:(CGRect)frame { 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; @@ -401,11 +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 - [self.reuseableViews removeObject:view]; + [set removeObject:view]; } return view; @@ -416,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]; } diff --git a/PSCollectionViewCell.h b/PSCollectionViewCell.h index c9ba811..99cf803 100644 --- a/PSCollectionViewCell.h +++ b/PSCollectionViewCell.h @@ -25,6 +25,7 @@ @interface PSCollectionViewCell : UIView +@property (nonatomic, strong) NSString *identifier;//to reuse the cell, set this field @property (nonatomic, strong) id object; - (void)prepareForReuse; diff --git a/PSCollectionViewCell.m b/PSCollectionViewCell.m index ff047ec..cccbbf5 100644 --- a/PSCollectionViewCell.m +++ b/PSCollectionViewCell.m @@ -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];