Ios UICollectionViewFlowLayout子类:重新加载数据后粘性单元格消失
我有以下Ios UICollectionViewFlowLayout子类:重新加载数据后粘性单元格消失,ios,uicollectionview,uicollectionviewlayout,Ios,Uicollectionview,Uicollectionviewlayout,我有以下UICollectionViewFlowLayout的子类: @implementation MyFlowLayout - (instancetype)init { self = [super init]; self.stickyIndexPaths = [NSMutableArray array]; return self; } - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds { ret
UICollectionViewFlowLayout
的子类:
@implementation MyFlowLayout
- (instancetype)init {
self = [super init];
self.stickyIndexPaths = [NSMutableArray array];
return self;
}
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
return YES;
}
- (NSArray*)layoutAttributesForElementsInRect:(CGRect)rect {
CGRect collectionViewArea = CGRectMake(0.f, 0.f, self.collectionView.contentSize.width, self.collectionView.contentSize.height);
NSArray *attributes = [super layoutAttributesForElementsInRect:collectionViewArea].copy;
for (UICollectionViewLayoutAttributes *item in attributes) {
[self makeStickyIfNeeded:item];
}
return attributes;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewLayoutAttributes *item = [super layoutAttributesForItemAtIndexPath:indexPath];
[self makeStickyIfNeeded:item];
return item;
}
- (void)makeStickyIfNeeded:(UICollectionViewLayoutAttributes*)item {
BOOL indexPathIsSticky = [self.stickyIndexPaths indexOfObjectPassingTest:^BOOL(NSIndexPath *i, NSUInteger idx, BOOL *stop) {
*stop = [i compare:item.indexPath] == NSOrderedSame;
return *stop;
}] != NSNotFound;
if (indexPathIsSticky) {
if (self.collectionView.contentOffset.y > item.frame.origin.y) {
CGRect f = item.frame;
f.origin.y = self.collectionView.contentOffset.y;
item.frame = f;
item.zIndex = 1.f;
}
}
else {
item.zIndex = 0.f;
}
}
从上面来看,我的想法是,我可以添加单元格的indexPaths并使它们“粘”到顶部(假设垂直流动),而粘性单元格下面的任何其他单元格都会在下面滚动。我正在用一个粘滞单元格测试这段代码,在这个单元格下面我有,比如说,30个其他单元格(足够使内容偏移量大于粘滞单元格的位置,这将导致它粘滞,而其余单元格在它下面滚动)。到目前为止还不错
但我注意到我的flow子类有两个副作用:
UICollectionView
时,粘性单元格下面的单元格不会出现。如果我稍微滚动一下集合视图,它们就会出现-reloadData
,当我获得正确的单元格数目的时候(比以前少一个),粘性标题将不再可见。与第一种情况类似,如果我轻推集合视图,它会立即再次出现有什么想法吗?我原以为新的contentOffset有问题,但事实似乎并非如此。到目前为止,我找到的唯一解决办法是在我执行
重新加载数据之后,立即告诉集合视图重新加载我正在使其变粘的项。也就是说,考虑到我的示例代码中布局对象具有stickyIndexPaths数组,我可以执行以下操作:
[self.collectionView reloadData];
[self.collectionView reloadItemsAtIndexPaths:self.layout.stickyIndexPaths];
(请注意,我在视图控制器中保留了对集合视图布局的引用)
这可以防止粘性细胞消失,但我还是不知道它们为什么会消失