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

Adds collectionView:layout:minimumColumnSpacingForSectionAtIndex to delegate #86

Merged
merged 1 commit into from
Apr 12, 2015
Merged
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
22 changes: 22 additions & 0 deletions CHTCollectionViewWaterfallLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,28 @@ extern NSString *const CHTCollectionElementKindSectionFooter;
*/
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;


/**
* Asks the delegate for the minimum spacing between colums in a secified section. If this method is not implemented, the
* minimumColumnSpacing property is used for all sections.
*
* @param collectionView
* The collection view object displaying the waterfall layout.
* @param collectionViewLayout
* The layout object requesting the information.
* @param section
* The index of the section whose minimum interitem spacing is being requested.
*
* @discussion
* If you do not implement this method, the waterfall layout uses the value in its minimumColumnSpacing property to determine the amount of space between columns in each section.
*
* @return
* The minimum spacing between each column.
*/
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumColumnSpacingForSectionAtIndex:(NSInteger)section;



@end

#pragma mark - CHTCollectionViewWaterfallLayout
Expand Down
17 changes: 14 additions & 3 deletions CHTCollectionViewWaterfallLayout.m
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,13 @@ - (CGFloat)itemWidthInSectionAtIndex:(NSInteger)section {
}
CGFloat width = self.collectionView.frame.size.width - sectionInset.left - sectionInset.right;
NSInteger columnCount = [self columnCountForSection:section];
return CHTFloorCGFloat((width - (columnCount - 1) * self.minimumColumnSpacing) / columnCount);

CGFloat columnSpacing = self.minimumColumnSpacing;
if ([self.delegate respondsToSelector:@selector(collectionView:layout:minimumColumnSpacingForSectionAtIndex:)]){
columnSpacing = [self.delegate collectionView:self.collectionView layout:self minimumColumnSpacingForSectionAtIndex:section];
}

return CHTFloorCGFloat((width - (columnCount - 1) * columnSpacing) / columnCount);
}

#pragma mark - Private Accessors
Expand Down Expand Up @@ -240,6 +246,11 @@ - (void)prepareLayout {
} else {
minimumInteritemSpacing = self.minimumInteritemSpacing;
}

CGFloat columnSpacing = self.minimumColumnSpacing;
if ([self.delegate respondsToSelector:@selector(collectionView:layout:minimumColumnSpacingForSectionAtIndex:)]){
columnSpacing = [self.delegate collectionView:self.collectionView layout:self minimumColumnSpacingForSectionAtIndex:section];
}

UIEdgeInsets sectionInset;
if ([self.delegate respondsToSelector:@selector(collectionView:layout:insetForSectionAtIndex:)]) {
Expand All @@ -250,7 +261,7 @@ - (void)prepareLayout {

CGFloat width = self.collectionView.frame.size.width - sectionInset.left - sectionInset.right;
NSInteger columnCount = [self columnCountForSection:section];
CGFloat itemWidth = CHTFloorCGFloat((width - (columnCount - 1) * self.minimumColumnSpacing) / columnCount);
CGFloat itemWidth = CHTFloorCGFloat((width - (columnCount - 1) * columnSpacing) / columnCount);

/*
* 2. Section header
Expand Down Expand Up @@ -299,7 +310,7 @@ - (void)prepareLayout {
for (idx = 0; idx < itemCount; idx++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:idx inSection:section];
NSUInteger columnIndex = [self nextColumnIndexForItem:idx inSection:section];
CGFloat xOffset = sectionInset.left + (itemWidth + self.minimumColumnSpacing) * columnIndex;
CGFloat xOffset = sectionInset.left + (itemWidth + columnSpacing) * columnIndex;
CGFloat yOffset = [self.columnHeights[section][columnIndex] floatValue];
CGSize itemSize = [self.delegate collectionView:self.collectionView layout:self sizeForItemAtIndexPath:indexPath];
CGFloat itemHeight = 0;
Expand Down