Skip to content
Open
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
28 changes: 19 additions & 9 deletions NHBalancedFlowLayout/NHBalancedFlowLayout.m
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ - (void)prepareLayout
[self clearItemFrames];

// create new item frame sections
_numberOfItemFrameSections = [self.collectionView numberOfSections];
_numberOfItemFrameSections = [self.collectionView.dataSource numberOfSectionsInCollectionView:self.collectionView];
_itemFrameSections = (CGRect **)malloc(sizeof(CGRect *) * _numberOfItemFrameSections);
for (int section = 0; section < [self.collectionView numberOfSections]; section++) {

for (int section = 0; section < _numberOfItemFrameSections; section++) {
// add new item frames array to sections array
NSInteger numberOfItemsInSections = [self.collectionView numberOfItemsInSection:section];
CGRect *itemFrames = (CGRect *)malloc(sizeof(CGRect) * numberOfItemsInSections);
Expand Down Expand Up @@ -167,14 +167,16 @@ - (CGSize)collectionViewContentSize
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSMutableArray *layoutAttributes = [NSMutableArray array];

for (NSInteger section = 0, n = [self.collectionView numberOfSections]; section < n; section++) {
NSInteger n = [self.collectionView.dataSource numberOfSectionsInCollectionView:self.collectionView];

for (NSInteger section = 0; section < n; section++) {
NSIndexPath *sectionIndexPath = [NSIndexPath indexPathForItem:0 inSection:section];

UICollectionViewLayoutAttributes *headerAttributes = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader
atIndexPath:sectionIndexPath];

if (! CGSizeEqualToSize(headerAttributes.frame.size, CGSizeZero) && CGRectIntersectsRect(headerAttributes.frame, rect)) {
CGSize size = headerAttributes.frame.size;
if (size.height != 0 && size.width != 0 && CGRectIntersectsRect(headerAttributes.frame, rect)) {
[layoutAttributes addObject:headerAttributes];
}

Expand All @@ -188,8 +190,8 @@ - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect

UICollectionViewLayoutAttributes *footerAttributes = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionFooter
atIndexPath:sectionIndexPath];

if (! CGSizeEqualToSize(footerAttributes.frame.size, CGSizeZero) && CGRectIntersectsRect(footerAttributes.frame, rect)) {
size = footerAttributes.frame.size;
if (size.width != 0 && size.height != 0 && CGRectIntersectsRect(footerAttributes.frame, rect)) {
[layoutAttributes addObject:footerAttributes];
}
}
Expand Down Expand Up @@ -253,7 +255,9 @@ - (CGRect)footerFrameForSection:(NSInteger)section
- (CGFloat)totalItemSizeForSection:(NSInteger)section preferredRowSize:(CGFloat)preferredRowSize
{
CGFloat totalItemSize = 0;
for (NSInteger i = 0, n = [self.collectionView numberOfItemsInSection:section]; i < n; i++) {
NSUInteger n = [self.collectionView.dataSource collectionView:self.collectionView numberOfItemsInSection:section];

for (NSInteger i = 0; i < n; i++) {
CGSize preferredSize = [self.delegate collectionView:self.collectionView layout:self preferredSizeForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:section]];

if (self.scrollDirection == UICollectionViewScrollDirectionVertical) {
Expand Down Expand Up @@ -282,6 +286,12 @@ - (NSArray *)weightsForItemsInSection:(NSInteger)section
- (void)setFrames:(CGRect *)frames forItemsInSection:(NSInteger)section numberOfRows:(NSUInteger)numberOfRows sectionOffset:(CGPoint)sectionOffset sectionSize:(CGSize *)sectionSize
{
NSArray *weights = [self weightsForItemsInSection:section];

if (weights.count == 0) {
*sectionSize = CGSizeZero;
return;
}

NSArray *partition = [NHLinearPartition linearPartitionForSequence:weights numberOfPartitions:numberOfRows];

int i = 0;
Expand Down