diff --git a/AsyncDisplayKit/ASCollectionView.mm b/AsyncDisplayKit/ASCollectionView.mm index b9f58ee5..088f8bac 100644 --- a/AsyncDisplayKit/ASCollectionView.mm +++ b/AsyncDisplayKit/ASCollectionView.mm @@ -481,14 +481,15 @@ static NSString * const kCellReuseIdentifier = @"_ASCollectionViewCell"; #pragma mark - #pragma mark Intercepted selectors. -- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath +- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { - _ASCollectionViewCell *cell = [self dequeueReusableCellWithReuseIdentifier:kCellReuseIdentifier forIndexPath:indexPath]; - - ASCellNode *node = [_dataController nodeAtIndexPath:indexPath]; - cell.node = node; - [_rangeController configureContentView:cell.contentView forCellNode:node]; - return cell; + _superIsPendingDataLoad = NO; + return [_dataController numberOfSections]; +} + +- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section +{ + return [_dataController numberOfRowsInSection:section]; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath @@ -505,17 +506,59 @@ static NSString * const kCellReuseIdentifier = @"_ASCollectionViewCell"; return view; } -- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView + + +- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { - _superIsPendingDataLoad = NO; - return [_dataController numberOfSections]; + _ASCollectionViewCell *cell = [self dequeueReusableCellWithReuseIdentifier:kCellReuseIdentifier forIndexPath:indexPath]; + + ASCellNode *node = [_dataController nodeAtIndexPath:indexPath]; + cell.node = node; + [_rangeController configureContentView:cell.contentView forCellNode:node]; + + if (ASRunningOnOS7()) { + // Even though UICV was introduced in iOS 6, and UITableView has always had the equivalent method, + // -willDisplayCell: was not introduced until iOS 8 for UICV. didEndDisplayingCell, however, is available. + [self collectionView:collectionView willDisplayCell:cell forItemAtIndexPath:indexPath]; + } + + return cell; } -- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section +- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath { - return [_dataController numberOfRowsInSection:section]; + [_rangeController visibleNodeIndexPathsDidChangeWithScrollDirection:[self scrollDirection]]; + + if ([_asyncDelegate respondsToSelector:@selector(collectionView:willDisplayNodeForItemAtIndexPath:)]) { + [_asyncDelegate collectionView:self willDisplayNodeForItemAtIndexPath:indexPath]; + } + + ASCellNode *cellNode = [self nodeForItemAtIndexPath:indexPath]; + if (cellNode.neverShowPlaceholders) { + [cellNode recursivelyEnsureDisplaySynchronously:YES]; + } } +- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath +{ + [_rangeController visibleNodeIndexPathsDidChangeWithScrollDirection:self.scrollDirection]; + + if ([_asyncDelegate respondsToSelector:@selector(collectionView:didEndDisplayingNode:forItemAtIndexPath:)]) { + ASCellNode *node = ((_ASCollectionViewCell *)cell).node; + ASDisplayNodeAssertNotNil(node, @"Expected node associated with removed cell not to be nil."); + [_asyncDelegate collectionView:self didEndDisplayingNode:node forItemAtIndexPath:indexPath]; + } +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" + if ([_asyncDelegate respondsToSelector:@selector(collectionView:didEndDisplayingNodeForItemAtIndexPath:)]) { + [_asyncDelegate collectionView:self didEndDisplayingNodeForItemAtIndexPath:indexPath]; + } +#pragma clang diagnostic pop +} + +#pragma mark - +#pragma mark Scroll Direction. + - (ASScrollDirection)scrollDirection { CGPoint scrollVelocity; @@ -577,37 +620,6 @@ static NSString * const kCellReuseIdentifier = @"_ASCollectionViewCell"; return scrollableDirection; } -- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath -{ - [_rangeController visibleNodeIndexPathsDidChangeWithScrollDirection:[self scrollDirection]]; - - if ([_asyncDelegate respondsToSelector:@selector(collectionView:willDisplayNodeForItemAtIndexPath:)]) { - [_asyncDelegate collectionView:self willDisplayNodeForItemAtIndexPath:indexPath]; - } - - ASCellNode *cellNode = [self nodeForItemAtIndexPath:indexPath]; - if (cellNode.neverShowPlaceholders) { - [cellNode recursivelyEnsureDisplaySynchronously:YES]; - } -} - -- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath -{ - [_rangeController visibleNodeIndexPathsDidChangeWithScrollDirection:self.scrollDirection]; - - if ([_asyncDelegate respondsToSelector:@selector(collectionView:didEndDisplayingNode:forItemAtIndexPath:)]) { - ASCellNode *node = ((_ASCollectionViewCell *)cell).node; - ASDisplayNodeAssertNotNil(node, @"Expected node associated with removed cell not to be nil."); - [_asyncDelegate collectionView:self didEndDisplayingNode:node forItemAtIndexPath:indexPath]; - } -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wdeprecated-declarations" - if ([_asyncDelegate respondsToSelector:@selector(collectionView:didEndDisplayingNodeForItemAtIndexPath:)]) { - [_asyncDelegate collectionView:self didEndDisplayingNodeForItemAtIndexPath:indexPath]; - } -#pragma clang diagnostic pop -} - - (void)layoutSubviews { if (_zeroContentInsets) { diff --git a/AsyncDisplayKit/Private/ASInternalHelpers.h b/AsyncDisplayKit/Private/ASInternalHelpers.h index 1f173bd9..11709e83 100644 --- a/AsyncDisplayKit/Private/ASInternalHelpers.h +++ b/AsyncDisplayKit/Private/ASInternalHelpers.h @@ -28,6 +28,8 @@ CGFloat ASCeilPixelValue(CGFloat f); CGFloat ASRoundPixelValue(CGFloat f); +BOOL ASRunningOnOS7(); + ASDISPLAYNODE_EXTERN_C_END /** diff --git a/AsyncDisplayKit/Private/ASInternalHelpers.mm b/AsyncDisplayKit/Private/ASInternalHelpers.mm index 557d5211..51e8d268 100644 --- a/AsyncDisplayKit/Private/ASInternalHelpers.mm +++ b/AsyncDisplayKit/Private/ASInternalHelpers.mm @@ -93,6 +93,16 @@ CGFloat ASRoundPixelValue(CGFloat f) return roundf(f * ASScreenScale()) / ASScreenScale(); } +BOOL ASRunningOnOS7() +{ + static BOOL isOS7 = NO; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + isOS7 = ([[UIDevice currentDevice].systemVersion floatValue] < 8.0); + }); + return isOS7; +} + @implementation NSIndexPath (ASInverseComparison) - (NSComparisonResult)asdk_inverseCompare:(NSIndexPath *)otherIndexPath