diff --git a/AsyncDisplayKit/ASCollectionView.mm b/AsyncDisplayKit/ASCollectionView.mm index b2f40d35e6..3dff9423af 100644 --- a/AsyncDisplayKit/ASCollectionView.mm +++ b/AsyncDisplayKit/ASCollectionView.mm @@ -228,13 +228,8 @@ static BOOL _isInterceptedSelector(SEL sel) // Register the default layout inspector delegate for flow layouts, custom layouts // will need to roll their own ASCollectionViewLayoutInspecting implementation. - if ([layout asdk_isFlowLayout]) { - _flowLayoutInspector = [[ASCollectionViewFlowLayoutInspector alloc] init]; - _flowLayoutInspector.layout = (UICollectionViewFlowLayout *)layout; - _flowLayoutInspector.collectionView = self; - _layoutDelegate = _flowLayoutInspector; - } - + _layoutDelegate = [self flowLayoutInspector]; + _registeredSupplementaryKinds = [NSMutableArray array]; self.backgroundColor = [UIColor whiteColor]; @@ -252,6 +247,21 @@ static BOOL _isInterceptedSelector(SEL sel) super.dataSource = nil; } +/** + * A layout inspector implementation specific for the sizing behavior of UICollectionViewFlowLayouts + * + * @discussion Will return nil if the current layout is not a flow layout + */ +- (ASCollectionViewFlowLayoutInspector *)flowLayoutInspector +{ + if (_flowLayoutInspector == nil) { + UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionViewLayout; + _flowLayoutInspector = [[ASCollectionViewFlowLayoutInspector alloc] initWithCollectionView:self + flowLayout:layout]; + } + return _flowLayoutInspector; +} + #pragma mark - #pragma mark Overrides. @@ -326,6 +336,8 @@ static BOOL _isInterceptedSelector(SEL sel) super.delegate = (id)_proxyDelegate; _asyncDelegateImplementsInsetSection = ([_asyncDelegate respondsToSelector:@selector(collectionView:layout:insetForSectionAtIndex:)] ? 1 : 0); } + + _flowLayoutInspector.collectionView = self; } - (void)setTuningParameters:(ASRangeTuningParameters)tuningParameters forRangeType:(ASLayoutRangeType)rangeType diff --git a/AsyncDisplayKit/Details/ASCollectionViewFlowLayoutInspector.h b/AsyncDisplayKit/Details/ASCollectionViewFlowLayoutInspector.h index 9adb624c96..94d65e2117 100644 --- a/AsyncDisplayKit/Details/ASCollectionViewFlowLayoutInspector.h +++ b/AsyncDisplayKit/Details/ASCollectionViewFlowLayoutInspector.h @@ -33,8 +33,10 @@ @interface ASCollectionViewFlowLayoutInspector : NSObject -@property (nonatomic, weak) ASCollectionView *collectionView; @property (nonatomic, weak) UICollectionViewFlowLayout *layout; +@property (nonatomic, weak) ASCollectionView *collectionView; + +- (instancetype)initWithCollectionView:(ASCollectionView *)collectionView flowLayout:(UICollectionViewFlowLayout *)flowLayout; - (ASSizeRange)collectionView:(ASCollectionView *)collectionView constrainedSizeForSupplementaryNodeOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath; diff --git a/AsyncDisplayKit/Details/ASCollectionViewFlowLayoutInspector.m b/AsyncDisplayKit/Details/ASCollectionViewFlowLayoutInspector.m index 41b39c144a..4c486d5d00 100644 --- a/AsyncDisplayKit/Details/ASCollectionViewFlowLayoutInspector.m +++ b/AsyncDisplayKit/Details/ASCollectionViewFlowLayoutInspector.m @@ -19,11 +19,25 @@ #pragma mark - Accessors -- (void)setLayout:(UICollectionViewFlowLayout *)layout +- (instancetype)initWithCollectionView:(ASCollectionView *)collectionView flowLayout:(UICollectionViewFlowLayout *)flowLayout { - _layout = layout; - _delegateImplementsReferenceSizeForHeader = [[self layoutDelegate] respondsToSelector:@selector(collectionView:layout:referenceSizeForHeaderInSection:)]; - _delegateImplementsReferenceSizeForFooter = [[self layoutDelegate] respondsToSelector:@selector(collectionView:layout:referenceSizeForFooterInSection:)]; + if (flowLayout == nil) { + return nil; + } + + self = [super init]; + if (self != nil) { + self.collectionView = collectionView; + _layout = flowLayout; + } + return self; +} + +- (void)setCollectionView:(ASCollectionView *)collectionView +{ + _collectionView = collectionView; + _delegateImplementsReferenceSizeForHeader = [[self layoutDelegate] respondsToSelector:@selector(collectionView:layout:referenceSizeForHeaderInSection:)]; + _delegateImplementsReferenceSizeForFooter = [[self layoutDelegate] respondsToSelector:@selector(collectionView:layout:referenceSizeForFooterInSection:)]; } #pragma mark - ASCollectionViewLayoutInspecting @@ -94,9 +108,9 @@ } } -- (id)layoutDelegate +- (id)layoutDelegate { - return (id)self.collectionView.delegate; + return (id)_collectionView.asyncDelegate; } @end