njdehoog / NHBalancedFlowLayout

UICollectionViewLayout subclass for displaying items of different sizes in a grid without wasting any visual space. Inspired by: http://www.crispymtn.com/stories/the-algorithm-for-a-perfectly-balanced-photo-gallery
MIT License
1.34k stars 106 forks source link

Fix#14 Prevent from a crash when no footer or no header. #16

Closed rvi closed 10 years ago

rvi commented 10 years ago

This methods should return nil for no footer or no header:

- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;

And this method, declared in UICollectionViewDatasource (implemented in the Demo project) :

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *view = nil;

    view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];

     if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
        view.frame = CGRectZero;
    }
    return view;
}

Note: The crash is not in this library, but in _createPreparedSupplementaryViewForElementOfKind: which is a private API of UICollectionView.

chiahsien commented 10 years ago

From Apple's document for - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath, it says

Return Value A configured supplementary view object. You must not return nil from this method.

The point is: You must not return nil for this method.

rvi commented 10 years ago

Thanks @chiahsien ! :+1:

That's not the code of the Layout (only the demo project). I updated my comment.

njdehoog commented 10 years ago

Thanks for the fix, and apologies for the delay in merging it back.

rvi commented 10 years ago

No problem ! Thanks for this awesome layout :)

Sent from my iPhone

On 16 févr. 2014, at 11:07, Niels de Hoog notifications@github.com wrote:

Thanks for the fix, and apologies for the delay in merging it back.

— Reply to this email directly or view it on GitHub.