From f9611356ad3fae2f9c3544a8b42e8a1a3b2c3620 Mon Sep 17 00:00:00 2001 From: Danny Date: Mon, 19 Nov 2012 12:50:37 +0900 Subject: [PATCH 1/2] Change PSCollectionViewDataSource protocol heightForViewAtIndex to collectionView:heightForViewAtIndex Can't control multiple collectionview --- BroBoard/BroBoard/PSViewController.m | 2 +- PSCollectionView.h | 2 +- PSCollectionView.m | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/BroBoard/BroBoard/PSViewController.m b/BroBoard/BroBoard/PSViewController.m index be0f65e..f2ff45c 100644 --- a/BroBoard/BroBoard/PSViewController.m +++ b/BroBoard/BroBoard/PSViewController.m @@ -147,7 +147,7 @@ - (PSCollectionViewCell *)collectionView:(PSCollectionView *)collectionView view return v; } -- (CGFloat)heightForViewAtIndex:(NSInteger)index { +- (CGFloat)collectionView:(PSCollectionView *)collectionView heightForViewAtIndex:(NSInteger)index { NSDictionary *item = [self.items objectAtIndex:index]; return [PSBroView heightForViewWithObject:item inColumnWidth:self.collectionView.colWidth]; diff --git a/PSCollectionView.h b/PSCollectionView.h index 0bbe669..df79f28 100644 --- a/PSCollectionView.h +++ b/PSCollectionView.h @@ -75,6 +75,6 @@ @required - (NSInteger)numberOfViewsInCollectionView:(PSCollectionView *)collectionView; - (PSCollectionViewCell *)collectionView:(PSCollectionView *)collectionView viewAtIndex:(NSInteger)index; -- (CGFloat)heightForViewAtIndex:(NSInteger)index; +- (CGFloat)collectionView:(PSCollectionView *)collectionView heightForViewAtIndex:(NSInteger)index; @end diff --git a/PSCollectionView.m b/PSCollectionView.m index 768c76c..8a37cde 100644 --- a/PSCollectionView.m +++ b/PSCollectionView.m @@ -295,7 +295,7 @@ - (void)relayoutViews { CGFloat left = kMargin + (col * kMargin) + (col * self.colWidth); CGFloat top = [[colOffsets objectAtIndex:col] floatValue]; - CGFloat colHeight = [self.collectionViewDataSource heightForViewAtIndex:i]; + CGFloat colHeight = [self.collectionViewDataSource collectionView:self heightForViewAtIndex:i]; if (colHeight == 0) { colHeight = self.colWidth; } From 5eea9030b021fc54ce5582b4d31bdc88b15b1167 Mon Sep 17 00:00:00 2001 From: Danny Date: Tue, 20 Nov 2012 15:32:02 +0900 Subject: [PATCH 2/2] Add sectionView such as tableView:viewForHeaderInSection: Fixed sample project's errors and apply sectionView --- BroBoard/BroBoard/PSBroView.m | 14 ++++++++++-- BroBoard/BroBoard/PSViewController.m | 23 +++++++++++++++++++- PSCollectionView.h | 1 + PSCollectionView.m | 32 ++++++++++++++++++++++++---- 4 files changed, 63 insertions(+), 7 deletions(-) diff --git a/BroBoard/BroBoard/PSBroView.m b/BroBoard/BroBoard/PSBroView.m index 71ae288..7ecf1ce 100644 --- a/BroBoard/BroBoard/PSBroView.m +++ b/BroBoard/BroBoard/PSBroView.m @@ -66,7 +66,12 @@ - (void)layoutSubviews { // Image CGFloat objectWidth = [[self.object objectForKey:@"width"] floatValue]; CGFloat objectHeight = [[self.object objectForKey:@"height"] floatValue]; - CGFloat scaledHeight = floorf(objectHeight / (objectWidth / width)); + CGFloat scaledHeight; + if (objectHeight == 0) { + scaledHeight = 0; + } else { + scaledHeight = floorf(objectHeight / (objectWidth / width)); + } self.imageView.frame = CGRectMake(left, top, width, scaledHeight); // Label @@ -98,7 +103,12 @@ + (CGFloat)heightForViewWithObject:(id)object inColumnWidth:(CGFloat)columnWidth // Image CGFloat objectWidth = [[object objectForKey:@"width"] floatValue]; CGFloat objectHeight = [[object objectForKey:@"height"] floatValue]; - CGFloat scaledHeight = floorf(objectHeight / (objectWidth / width)); + CGFloat scaledHeight; + if (objectHeight == 0) { + scaledHeight = 0; + } else { + scaledHeight = floorf(objectHeight / (objectWidth / width)); + } height += scaledHeight; // Label diff --git a/BroBoard/BroBoard/PSViewController.m b/BroBoard/BroBoard/PSViewController.m index f2ff45c..9bcb764 100644 --- a/BroBoard/BroBoard/PSViewController.m +++ b/BroBoard/BroBoard/PSViewController.m @@ -88,6 +88,27 @@ - (void)viewDidLoad { self.collectionView.numColsLandscape = 3; } + UIView *sectionView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.view.bounds.size.width, 22.0f)]; + [sectionView setBackgroundColor:[UIColor blackColor]]; + [sectionView setAlpha:0.75f]; + UILabel *sectionViewlabel = [[UILabel alloc] initWithFrame:sectionView.bounds]; + [sectionViewlabel setTextColor:[UIColor whiteColor]]; + [sectionViewlabel setBackgroundColor:[UIColor clearColor]]; + [sectionViewlabel setText:@"I'm section view"]; + [sectionViewlabel setTextAlignment:NSTextAlignmentCenter]; + [sectionView addSubview:sectionViewlabel]; + [self.collectionView setSectionView:sectionView]; + + UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.view.frame.size.width, 50.0f)]; + [headerView setBackgroundColor:[UIColor orangeColor]]; + UILabel *headerViewLabel = [[UILabel alloc] initWithFrame:sectionView.bounds]; + [headerViewLabel setTextColor:[UIColor blackColor]]; + [headerViewLabel setBackgroundColor:[UIColor clearColor]]; + [headerViewLabel setText:@"I'm header view"]; + [headerViewLabel setTextAlignment:NSTextAlignmentCenter]; + [headerView addSubview:headerViewLabel]; + [self.collectionView setHeaderView:headerView]; + UILabel *loadingLabel = [[UILabel alloc] initWithFrame:self.collectionView.bounds]; loadingLabel.text = @"Loading..."; loadingLabel.textAlignment = UITextAlignmentCenter; @@ -110,7 +131,7 @@ - (void)loadDataSource { if (!error && responseCode == 200) { id res = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; if (res && [res isKindOfClass:[NSDictionary class]]) { - self.items = [res objectForKey:@"gallery"]; + self.items = [res objectForKey:@"data"]; [self dataSourceDidLoad]; } else { [self dataSourceDidError]; diff --git a/PSCollectionView.h b/PSCollectionView.h index df79f28..1a7eb8c 100644 --- a/PSCollectionView.h +++ b/PSCollectionView.h @@ -32,6 +32,7 @@ #pragma mark - Public Properties @property (nonatomic, retain) UIView *headerView; +@property (nonatomic, retain) UIView *sectionView; @property (nonatomic, retain) UIView *footerView; @property (nonatomic, retain) UIView *emptyView; @property (nonatomic, retain) UIView *loadingView; diff --git a/PSCollectionView.m b/PSCollectionView.m index 8a37cde..fb223bd 100644 --- a/PSCollectionView.m +++ b/PSCollectionView.m @@ -144,6 +144,7 @@ @implementation PSCollectionView // Public Views @synthesize headerView = _headerView, +sectionView = _sectionView, footerView = _footerView, emptyView = _emptyView, loadingView = _loadingView; @@ -194,6 +195,7 @@ - (void)dealloc { // release retains self.headerView = nil; + self.sectionView = nil; self.footerView = nil; self.emptyView = nil; self.loadingView = nil; @@ -235,6 +237,15 @@ - (void)layoutSubviews { } else { [self removeAndAddCellsIfNecessary]; } + + if (self.sectionView) { + CGFloat top = (self.headerView) ? self.headerView.top + self.headerView.height : 0.0f; + if (self.contentOffset.y >= top) { + self.sectionView.top = self.contentOffset.y; + } else { + self.sectionView.top = top; + } + } } - (void)relayoutViews { @@ -258,17 +269,25 @@ - (void)relayoutViews { NSInteger numViews = [self.collectionViewDataSource numberOfViewsInCollectionView:self]; CGFloat totalHeight = 0.0; - CGFloat top = kMargin; + CGFloat top = 0.0; // Add headerView if it exists if (self.headerView) { - self.headerView.top = kMargin; + self.headerView.top = 0; top = self.headerView.top; [self addSubview:self.headerView]; top += self.headerView.height; - top += kMargin; } + // Add sectionView + if (self.sectionView) { + self.sectionView.top = top; + [self addSubview:self.sectionView]; + top = self.sectionView.top + self.sectionView.height; + } + + top += kMargin; + if (numViews > 0) { // This array determines the last height offset on a column NSMutableArray *colOffsets = [NSMutableArray arrayWithCapacity:self.numCols]; @@ -400,7 +419,12 @@ - (void)removeAndAddCellsIfNecessary { // Only add views if not visible PSCollectionViewCell *newView = [self.collectionViewDataSource collectionView:self viewAtIndex:i]; newView.frame = CGRectFromString([self.indexToRectMap objectForKey:key]); - [self addSubview:newView]; + if (self.sectionView) { + [self insertSubview:newView belowSubview:self.sectionView]; + } else { + [self addSubview:newView]; + + } // Setup gesture recognizer if ([newView.gestureRecognizers count] == 0) {