From b23d6f901d722b07f3099b74789f588a0a01d907 Mon Sep 17 00:00:00 2001 From: llwijvbw123 Date: Thu, 27 May 2021 09:47:34 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3header=20=E5=9B=A0=E9=87=8D?= =?UTF-8?q?=E7=94=A8=E6=8F=90=E5=89=8D=E9=9A=90=E8=97=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 借鉴代码:https://github.com/HebeTienCoder/XLPlainFlowLayout/blob/master/XLPlainFlowLayout/XLPlainFlowLayout.m --- .../XPCollectionViewWaterfallFlowLayout.m | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) 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) {