Skip to content

Commit

Permalink
Always mark an element visible even if it is backed by an UIKit / non…
Browse files Browse the repository at this point in the history
…-_ASCollection* view
  • Loading branch information
nguyenhuy committed Jul 6, 2017
1 parent bd31b05 commit 2bcf410
Showing 1 changed file with 25 additions and 24 deletions.
49 changes: 25 additions & 24 deletions Source/ASCollectionView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1069,12 +1069,7 @@ - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICol
[(id <ASCollectionDelegateInterop>)_asyncDelegate collectionView:collectionView willDisplayCell:rawCell forItemAtIndexPath:indexPath];
}

_ASCollectionViewCell *cell = ASDynamicCastStrict(rawCell, _ASCollectionViewCell);
if (cell == nil) {
[_rangeController setNeedsUpdate];
return;
}

// Always add the element to the visible set even if it is backed by an UIKit / non-_ASCollection* view
ASCollectionElement *element = [_dataController.visibleMap elementForItemAtIndexPath:indexPath];
if (element) {
[_visibleElements addObject:element];
Expand All @@ -1083,6 +1078,12 @@ - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICol
return;
}

_ASCollectionViewCell *cell = ASDynamicCastStrict(rawCell, _ASCollectionViewCell);
if (cell == nil) {
[_rangeController setNeedsUpdate];
return;
}

ASCellNode *cellNode = element.node;
cellNode.scrollView = collectionView;

Expand Down Expand Up @@ -1125,12 +1126,7 @@ - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(
[(id <ASCollectionDelegateInterop>)_asyncDelegate collectionView:collectionView didEndDisplayingCell:rawCell forItemAtIndexPath:indexPath];
}

_ASCollectionViewCell *cell = ASDynamicCastStrict(rawCell, _ASCollectionViewCell);
if (cell == nil) {
[_rangeController setNeedsUpdate];
return;
}

// Always remove the element from the visible set even if it is backed by an UIKit / non-_ASCollection* view
ASCollectionElement *element = [_dataController.visibleMap elementForItemAtIndexPath:indexPath];
if (element) {
[_visibleElements removeObject:element];
Expand All @@ -1139,6 +1135,12 @@ - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(
return;
}

_ASCollectionViewCell *cell = ASDynamicCastStrict(rawCell, _ASCollectionViewCell);
if (cell == nil) {
[_rangeController setNeedsUpdate];
return;
}

ASCellNode *cellNode = element.node;

if (_asyncDelegateFlags.collectionNodeDidEndDisplayingItem) {
Expand All @@ -1162,11 +1164,7 @@ - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(

- (void)collectionView:(UICollectionView *)collectionView willDisplaySupplementaryView:(UICollectionReusableView *)rawView forElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath
{
_ASCollectionReusableView *view = ASDynamicCastStrict(rawView, _ASCollectionReusableView);
if (view == nil) {
return;
}

// Always add the element to the visible set even if it is backed by an UIKit / non-_ASCollection* view
ASCollectionElement *element = [_dataController.visibleMap supplementaryElementOfKind:elementKind atIndexPath:indexPath];
if (element) {
[_visibleElements addObject:element];
Expand All @@ -1175,13 +1173,15 @@ - (void)collectionView:(UICollectionView *)collectionView willDisplaySupplementa
return;
}

// Under iOS 10+, cells may be removed/re-added to the collection view without
// receiving prepareForReuse/applyLayoutAttributes, as an optimization for e.g.
// if the user is scrolling back and forth across a small set of items.
// In this case, we have to fetch the layout attributes manually.
// This may be possible under iOS < 10 but it has not been observed yet.
if (view.layoutAttributes == nil) {
view.layoutAttributes = [collectionView layoutAttributesForSupplementaryElementOfKind:elementKind atIndexPath:indexPath];
if (_ASCollectionReusableView *view = ASDynamicCastStrict(rawView, _ASCollectionReusableView)) {
// Under iOS 10+, cells may be removed/re-added to the collection view without
// receiving prepareForReuse/applyLayoutAttributes, as an optimization for e.g.
// if the user is scrolling back and forth across a small set of items.
// In this case, we have to fetch the layout attributes manually.
// This may be possible under iOS < 10 but it has not been observed yet.
if (view.layoutAttributes == nil) {
view.layoutAttributes = [collectionView layoutAttributesForSupplementaryElementOfKind:elementKind atIndexPath:indexPath];
}
}

if (_asyncDelegateFlags.collectionNodeWillDisplaySupplementaryElement) {
Expand All @@ -1194,6 +1194,7 @@ - (void)collectionView:(UICollectionView *)collectionView willDisplaySupplementa

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingSupplementaryView:(UICollectionReusableView *)rawView forElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath
{
// Always remove the element from the visible set even if it is backed by an UIKit / non-_ASCollection* view
ASCollectionElement *element = [_dataController.visibleMap supplementaryElementOfKind:elementKind atIndexPath:indexPath];
if (element) {
[_visibleElements removeObject:element];
Expand Down

0 comments on commit 2bcf410

Please sign in to comment.