Skip to content
This repository has been archived by the owner on Oct 10, 2018. It is now read-only.

Commit

Permalink
Merge pull request #4 from kevin-zqw/master
Browse files Browse the repository at this point in the history
Improvement and bugfix
  • Loading branch information
darrarski committed Jul 10, 2015
2 parents eae3bec + 4145b90 commit e89c5f8
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 23 deletions.
12 changes: 12 additions & 0 deletions DRCollectionViewTableLayout/DRCollectionViewTableLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#import <UIKit/UIKit.h>

extern const NSInteger DRTopLeftColumnHeaderIndex;

/**
* Supplementary View kind for column headers
*/
Expand Down Expand Up @@ -123,6 +125,16 @@ static NSString * const DRCollectionViewTableLayoutSupplementaryViewRowHeader =
*/
@property (nonatomic, assign) CGFloat verticalSpacing;

/**
* Vertical spacing between cells
*/
@property (nonatomic, assign) CGFloat verticalSectionSpacing;

/**
* Has top left supplementary column header view
*/
@property (nonatomic, assign) BOOL hasTopLeftColumnHeaderView;

/**
* Initialize with delegate
*
Expand Down
72 changes: 49 additions & 23 deletions DRCollectionViewTableLayout/DRCollectionViewTableLayout.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#import "DRCollectionViewTableLayout.h"

const NSInteger DRTopLeftColumnHeaderIndex = -1;

#pragma mark - DRCollectionViewTableLayoutInvalidationContext

@interface DRCollectionViewTableLayoutInvalidationContext : UICollectionViewLayoutInvalidationContext
Expand Down Expand Up @@ -38,6 +40,7 @@ - (id)initWithDelegate:(id<DRCollectionViewTableLayoutDelegate>)delegate
{
if (self = [super init]) {
_delegate = delegate;
_verticalSectionSpacing = 50.f;
}
return self;
}
Expand Down Expand Up @@ -239,15 +242,17 @@ - (NSArray *)layoutAttributesForSupplementaryViews
numberOfColumnsInSection:sectionIdx];


if ([self heightForColumnHeaderInSection:sectionIdx] > 0) {
BOOL hasColumnHeader = ([self heightForColumnHeaderInSection:sectionIdx] > 0);
if (hasColumnHeader) {
for (NSUInteger columnIdx = 0; columnIdx < columnsCount; columnIdx++) {
[layoutAttributes addObject:[self layoutAttributesForSupplementaryViewOfKind:DRCollectionViewTableLayoutSupplementaryViewColumnHeader
atIndexPath:[NSIndexPath indexPathForItem:columnIdx
inSection:sectionIdx]]];
}
}

if ([self widthForRowHeaderInSection:sectionIdx] > 0) {
BOOL hasRowHeader = ([self widthForRowHeaderInSection:sectionIdx] > 0);
if (hasRowHeader) {
NSUInteger itemsCount = [self.collectionView.dataSource collectionView:self.collectionView
numberOfItemsInSection:sectionIdx];
NSUInteger rowsCount = ceilf((float)itemsCount / (float)columnsCount);
Expand All @@ -257,6 +262,12 @@ - (NSArray *)layoutAttributesForSupplementaryViews
inSection:sectionIdx]]];
}
}

if (hasColumnHeader && hasRowHeader && self.hasTopLeftColumnHeaderView) {
[layoutAttributes addObject:[self layoutAttributesForSupplementaryViewOfKind:DRCollectionViewTableLayoutSupplementaryViewColumnHeader
atIndexPath:[NSIndexPath indexPathForItem:DRTopLeftColumnHeaderIndex
inSection:sectionIdx]]];
}
}
_layoutAttributesForSupplementaryViews = [NSArray arrayWithArray:layoutAttributes];
}
Expand Down Expand Up @@ -327,6 +338,7 @@ - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSInde
y += self.verticalSpacing / 2.f;
}
}
y += (indexPath.section * self.verticalSectionSpacing);

// compute item width
CGFloat width = [self.delegate collectionView:self.collectionView
Expand Down Expand Up @@ -361,31 +373,38 @@ - (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind
UICollectionViewLayoutAttributes *currentItemAttributes = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:kind withIndexPath:indexPath];

if ([kind isEqualToString:DRCollectionViewTableLayoutSupplementaryViewColumnHeader]) {

// get header column index
NSUInteger currentColumn = [self columnNumberForHeaderIndexPath:indexPath];

// compute width
CGFloat width = [self.delegate collectionView:self.collectionView
tableLayout:self
widthForColumn:indexPath.row
inSection:indexPath.section];

// compute height
CGFloat height = [self heightForColumnHeaderInSection:indexPath.section];
CGFloat width;

// compute x position
CGFloat x = 0;
CGFloat rowHeaderWidth = [self widthForRowHeaderInSection:indexPath.section];
if (rowHeaderWidth > 0) {
x += rowHeaderWidth + (self.horizontalSpacing / 2.f);
}
for (NSUInteger columnIdx = 0; columnIdx < currentColumn; columnIdx++) {
x += [self.delegate collectionView:self.collectionView
tableLayout:self
widthForColumn:columnIdx
inSection:indexPath.section];
x += self.horizontalSpacing / 2.f;
if (indexPath.item == DRTopLeftColumnHeaderIndex) {
x = 0;
width = [self.delegate collectionView:self.collectionView
tableLayout:self
widthForRowHeaderInSection:indexPath.section];
} else {
// compute x position
CGFloat rowHeaderWidth = [self widthForRowHeaderInSection:indexPath.section];
if (rowHeaderWidth > 0) {
x += rowHeaderWidth + (self.horizontalSpacing / 2.f);
}
for (NSUInteger columnIdx = 0; columnIdx < currentColumn; columnIdx++) {
x += [self.delegate collectionView:self.collectionView
tableLayout:self
widthForColumn:columnIdx
inSection:indexPath.section];
x += self.horizontalSpacing / 2.f;
}

// compute width
width = [self.delegate collectionView:self.collectionView
tableLayout:self
widthForColumn:indexPath.row
inSection:indexPath.section];
}

// compute y position
Expand All @@ -405,6 +424,7 @@ - (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind
y += self.verticalSpacing / 2.f;
}
}
y += (indexPath.section * self.verticalSectionSpacing);

// stick column header to top edge
if ([self stickyColumnHeadersInSection:indexPath.section]) {
Expand All @@ -426,9 +446,12 @@ - (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind
maxY += self.verticalSpacing / 2.f;
}
}
maxY += (indexPath.section * self.verticalSectionSpacing);
maxY -= height + (self.verticalSpacing / 2.f);
if (y < self.collectionView.contentOffset.y) {
y = MIN(maxY, CGRectGetMinY(self.collectionView.bounds) + self.collectionView.contentInset.top);

CGFloat stickyY = self.collectionView.contentOffset.y + self.collectionView.contentInset.top;
if (y < stickyY) {
y = MIN(maxY, stickyY);
}
}

Expand All @@ -443,7 +466,9 @@ - (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind

// stick header to left edge
if ([self stickyRowHeadersInSection:indexPath.section]) {
x = CGRectGetMinX(self.collectionView.bounds) + self.collectionView.contentInset.left;
CGFloat stickyX = CGRectGetMinX(self.collectionView.bounds) + self.collectionView.contentInset.left;
if (x < stickyX)
x = stickyX;
}

// compute y position
Expand Down Expand Up @@ -471,6 +496,7 @@ - (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind
y += self.verticalSpacing / 2.f;
}
}
y += (indexPath.section * self.verticalSectionSpacing);

// compute width
CGFloat width = [self widthForRowHeaderInSection:indexPath.section];
Expand Down

0 comments on commit e89c5f8

Please sign in to comment.