diff --git a/Example/WaterfallLayout/XPCollectionViewWaterfallFlowLayout.m b/Example/WaterfallLayout/XPCollectionViewWaterfallFlowLayout.m index 2494f68..49c4ae0 100644 --- a/Example/WaterfallLayout/XPCollectionViewWaterfallFlowLayout.m +++ b/Example/WaterfallLayout/XPCollectionViewWaterfallFlowLayout.m @@ -146,6 +146,41 @@ - (CGSize)collectionViewContentSize { [result addObject:attribute]; } }]; + //创建存索引的数组,无符号(正整数),无序(不能通过下标取值),不可重复(重复的话会自动过滤) + NSMutableIndexSet *noneHeaderSections = [NSMutableIndexSet indexSet]; + //遍历superArray,得到一个当前屏幕中所有的section数组 + for (UICollectionViewLayoutAttributes *attributes in result) + { + //如果当前的元素分类是一个cell,将cell所在的分区section加入数组,重复的话会自动过滤 + if (attributes.representedElementCategory == UICollectionElementCategoryCell) + { + [noneHeaderSections addIndex:attributes.indexPath.section]; + } + } + + //遍历superArray,将当前屏幕中拥有的header的section从数组中移除,得到一个当前屏幕中没有header的section数组 + //正常情况下,随着手指往上移,header脱离屏幕会被系统回收而cell尚在,也会触发该方法 + for (UICollectionViewLayoutAttributes *attributes in result) { + //如果当前的元素是一个header,将header所在的section从数组中移除 + if ([attributes.representedElementKind isEqualToString:UICollectionElementKindSectionHeader]) { + [noneHeaderSections removeIndex:attributes.indexPath.section]; + } + } + + //遍历当前屏幕中没有header的section数组 + [noneHeaderSections enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) { + + //取到当前section中第一个item的indexPath + NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:idx]; + //获取当前section在正常情况下已经离开屏幕的header结构信息 + UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:indexPath]; + + //如果当前分区确实有因为离开屏幕而被系统回收的header + if (attributes) { + //将该header结构信息重新加入到superArray中去 + [result addObject:attributes]; + } + }]; // Header view hover. if (_sectionHeadersPinToVisibleBounds) {