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

Change PSCollectionViewDataSource protocol for multiple collectionview #16

Open
wants to merge 2 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: 12 additions & 2 deletions BroBoard/BroBoard/PSBroView.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
25 changes: 23 additions & 2 deletions BroBoard/BroBoard/PSViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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];
Expand Down Expand Up @@ -147,7 +168,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];
Expand Down
3 changes: 2 additions & 1 deletion PSCollectionView.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -75,6 +76,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
34 changes: 29 additions & 5 deletions PSCollectionView.m
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ @implementation PSCollectionView
// Public Views
@synthesize
headerView = _headerView,
sectionView = _sectionView,
footerView = _footerView,
emptyView = _emptyView,
loadingView = _loadingView;
Expand Down Expand Up @@ -194,6 +195,7 @@ - (void)dealloc {

// release retains
self.headerView = nil;
self.sectionView = nil;
self.footerView = nil;
self.emptyView = nil;
self.loadingView = nil;
Expand Down Expand Up @@ -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 {
Expand All @@ -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];
Expand All @@ -295,7 +314,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;
}
Expand Down Expand Up @@ -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) {
Expand Down