diff --git a/PSCollectionView.h b/PSCollectionView.h index 0bbe669..a569274 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 @@ -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 768c76c..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]; @@ -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 @@ -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) 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,23 +186,26 @@ - (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; 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 +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]; } @@ -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) { @@ -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); @@ -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; @@ -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; @@ -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]; } diff --git a/PSCollectionViewCell.h b/PSCollectionViewCell.h index b37551b..99cf803 100644 --- a/PSCollectionViewCell.h +++ b/PSCollectionViewCell.h @@ -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; diff --git a/PSCollectionViewCell.m b/PSCollectionViewCell.m index 4ffb17f..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]; @@ -39,10 +39,6 @@ - (id)initWithFrame:(CGRect)frame { return self; } -- (void)dealloc { - self.object = nil; - [super dealloc]; -} - (void)prepareForReuse { }