Implement UICollectionViewFlowLayout heuristics in a default inspector

This commit is contained in:
Levi McCallum
2015-09-29 18:01:20 -07:00
parent a659ce7bf8
commit 73ddebed81
3 changed files with 99 additions and 28 deletions

View File

@@ -688,21 +688,6 @@ static BOOL _isInterceptedSelector(SEL sel)
return [_asyncDataSource collectionView:self numberOfItemsInSection:section]; return [_asyncDataSource collectionView:self numberOfItemsInSection:section];
} }
- (ASSizeRange)dataController:(ASCollectionDataController *)dataController constrainedSizeForSupplementaryNodeOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
return [self.layoutDelegate collectionView:self constrainedSizeForSupplementaryNodeOfKind:kind atIndexPath:indexPath];
}
- (NSUInteger)dataController:(ASCollectionDataController *)dataController supplementaryViewsOfKind:(NSString *)kind inSection:(NSUInteger)section
{
return [self.layoutDelegate collectionView:self supplementaryViewsOfKind:kind inSection:section];
}
- (NSUInteger)dataController:(ASCollectionDataController *)dataController numberOfSectionsForSupplementaryKind:(NSString *)kind;
{
return [self.layoutDelegate collectionView:self numberOfSectionsForSupplementaryKind:kind];
}
- (NSUInteger)numberOfSectionsInDataController:(ASDataController *)dataController { - (NSUInteger)numberOfSectionsInDataController:(ASDataController *)dataController {
if ([_asyncDataSource respondsToSelector:@selector(numberOfSectionsInCollectionView:)]) { if ([_asyncDataSource respondsToSelector:@selector(numberOfSectionsInCollectionView:)]) {
return [_asyncDataSource numberOfSectionsInCollectionView:self]; return [_asyncDataSource numberOfSectionsInCollectionView:self];
@@ -731,8 +716,24 @@ static BOOL _isInterceptedSelector(SEL sel)
} }
} }
#pragma mark - #pragma mark - ASCollectionViewDataControllerSource Supplementary view support
#pragma mark ASRangeControllerDelegate.
- (ASSizeRange)dataController:(ASCollectionDataController *)dataController constrainedSizeForSupplementaryNodeOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
return [_layoutDelegate collectionView:self constrainedSizeForSupplementaryNodeOfKind:kind atIndexPath:indexPath];
}
- (NSUInteger)dataController:(ASCollectionDataController *)dataController supplementaryViewsOfKind:(NSString *)kind inSection:(NSUInteger)section
{
return [_layoutDelegate collectionView:self supplementaryViewsOfKind:kind inSection:section];
}
- (NSUInteger)dataController:(ASCollectionDataController *)dataController numberOfSectionsForSupplementaryKind:(NSString *)kind;
{
return [_layoutDelegate collectionView:self numberOfSectionsForSupplementaryKind:kind];
}
#pragma mark - ASRangeControllerDelegate.
- (void)rangeControllerBeginUpdates:(ASRangeController *)rangeController { - (void)rangeControllerBeginUpdates:(ASRangeController *)rangeController {
ASDisplayNodeAssertMainThread(); ASDisplayNodeAssertMainThread();

View File

@@ -14,10 +14,19 @@
@protocol ASCollectionViewLayoutInspecting <NSObject> @protocol ASCollectionViewLayoutInspecting <NSObject>
/**
* Asks the inspector
*/
- (ASSizeRange)collectionView:(ASCollectionView *)collectionView constrainedSizeForSupplementaryNodeOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath; - (ASSizeRange)collectionView:(ASCollectionView *)collectionView constrainedSizeForSupplementaryNodeOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;
/**
* Asks the inspector for the number of supplementary sections in the collection view for the given kind.
*/
- (NSUInteger)collectionView:(ASCollectionView *)collectionView numberOfSectionsForSupplementaryKind:(NSString *)kind; - (NSUInteger)collectionView:(ASCollectionView *)collectionView numberOfSectionsForSupplementaryKind:(NSString *)kind;
/**
* Asks the inspector for the number of supplementary views for the given kind in the specified section.
*/
- (NSUInteger)collectionView:(ASCollectionView *)collectionView supplementaryViewsOfKind:(NSString *)kind inSection:(NSUInteger)section; - (NSUInteger)collectionView:(ASCollectionView *)collectionView supplementaryViewsOfKind:(NSString *)kind inSection:(NSUInteger)section;
@end @end

View File

@@ -6,32 +6,93 @@
// //
// //
#import <UIKit/UIKit.h>
#import "ASCollectionViewFlowLayoutInspector.h" #import "ASCollectionViewFlowLayoutInspector.h"
#import "ASCollectionView.h" #import "ASCollectionView.h"
@implementation ASCollectionViewFlowLayoutInspector @implementation ASCollectionViewFlowLayoutInspector {
BOOL _delegateImplementsReferenceSizeForHeader;
BOOL _delegateImplementsReferenceSizeForFooter;
}
#pragma mark - Accessors
- (void)setLayout:(UICollectionViewFlowLayout *)layout
{
_layout = layout;
_delegateImplementsReferenceSizeForHeader = [[self layoutDelegate] respondsToSelector:@selector(collectionView:layout:referenceSizeForHeaderInSection:)];
_delegateImplementsReferenceSizeForFooter = [[self layoutDelegate] respondsToSelector:@selector(collectionView:layout:referenceSizeForFooterInSection:)];
}
#pragma mark - ASCollectionViewLayoutInspecting
- (ASSizeRange)collectionView:(ASCollectionView *)collectionView constrainedSizeForSupplementaryNodeOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath - (ASSizeRange)collectionView:(ASCollectionView *)collectionView constrainedSizeForSupplementaryNodeOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{ {
// TODO: Implement some heuristic that follows the width/height constraints of header and footer supplementary views CGSize constrainedSize = CGSizeMake(FLT_MAX, FLT_MAX);
return ASSizeRangeMake(CGSizeZero, CGSizeMake(FLT_MAX, FLT_MAX)); CGSize supplementarySize = [self sizeForSupplementaryViewOfKind:kind inSection:indexPath.section];
if (_layout.scrollDirection == UICollectionViewScrollDirectionVertical) {
constrainedSize.height = supplementarySize.height;
} else {
constrainedSize.width = supplementarySize.width;
}
return ASSizeRangeMake(CGSizeZero, constrainedSize);
} }
- (NSUInteger)collectionView:(ASCollectionView *)collectionView numberOfSectionsForSupplementaryKind:(NSString *)kind { - (NSUInteger)collectionView:(ASCollectionView *)collectionView numberOfSectionsForSupplementaryKind:(NSString *)kind
return self.collectionView.numberOfSections; {
return [collectionView.asyncDataSource numberOfSectionsInCollectionView:collectionView];
} }
- (NSUInteger)collectionView:(ASCollectionView *)collectionView supplementaryViewsOfKind:(NSString *)kind inSection:(NSUInteger)section - (NSUInteger)collectionView:(ASCollectionView *)collectionView supplementaryViewsOfKind:(NSString *)kind inSection:(NSUInteger)section
{ {
NSUInteger count = 0; return [self layoutHasSupplementaryViewOfKind:kind inSection:section] ? 1 : 0;
if (self.layout.headerReferenceSize.width > 0 || self.layout.headerReferenceSize.height > 0) {
count++;
} }
if (self.layout.footerReferenceSize.width > 0 || self.layout.footerReferenceSize.height > 0) {
count++; #pragma mark - Private helpers
- (CGSize)sizeForSupplementaryViewOfKind:(NSString *)kind inSection:(NSUInteger)section
{
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
if (_delegateImplementsReferenceSizeForHeader) {
return [[self layoutDelegate] collectionView:_collectionView layout:_layout referenceSizeForHeaderInSection:section];
} else {
return [self.layout headerReferenceSize];
} }
return count; } else if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
if (_delegateImplementsReferenceSizeForFooter) {
return [[self layoutDelegate] collectionView:_collectionView layout:_layout referenceSizeForFooterInSection:section];
} else {
return [self.layout footerReferenceSize];
}
} else {
return CGSizeZero;
}
}
- (BOOL)layoutHasSupplementaryViewOfKind:(NSString *)kind inSection:(NSUInteger)section
{
CGSize size = [self sizeForSupplementaryViewOfKind:kind inSection:section];
if ([self usedLayoutValueForSize:size] > 0) {
return YES;
} else {
return NO;
}
}
- (CGFloat)usedLayoutValueForSize:(CGSize)size
{
if (_layout.scrollDirection == UICollectionViewScrollDirectionVertical) {
return size.height;
} else {
return size.width;
}
}
- (id<UICollectionViewDelegateFlowLayout>)layoutDelegate
{
return (id<UICollectionViewDelegateFlowLayout>)self.collectionView.delegate;
} }
@end @end