下面是我的自定义布局类的 layoutAttributesForELElementsInRect
方法。对于第 0 节,layoutAttributes
为 nil,因此不会显示第一个节标题。
“Section in layoutAttributedForElementsInRect: 0”和“Entered here”被打印出来。
对于其余部分,layoutAttributes
不为零。
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSMutableArray *answer = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
UICollectionView *const cv = self.collectionView;
CGPoint const contentOffset = cv.contentOffset;
NSMutableIndexSet *missingSections = [NSMutableIndexSet indexSet];
for (UICollectionViewLayoutAttributes *layoutAttributes in answer)
{
if (layoutAttributes.representedElementCategory == UICollectionElementCategoryCell)
{
[missingSections addIndex:layoutAttributes.indexPath.section];
}
}
for (UICollectionViewLayoutAttributes *layoutAttributes in answer)
{
if ([layoutAttributes.representedElementKind isEqualToString:UICollectionElementKindSectionHeader])
{
[missingSections removeIndex:layoutAttributes.indexPath.section];
}
}
[missingSections enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:idx];
NSLog(@"Section in layoutAttributedForElementsInRect: %ld", indexPath.section);
UICollectionViewLayoutAttributes *layoutAttributes = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:indexPath];
if (layoutAttributes != nil)
{
[answer addObject:layoutAttributes];
}
else
{
NSLog(@"Entered here");
}
}];
for (UICollectionViewLayoutAttributes *layoutAttributes in answer)
{
if ([layoutAttributes.representedElementKind isEqualToString:UICollectionElementKindSectionHeader])
{
NSInteger section = layoutAttributes.indexPath.section;
NSInteger numberOfItemsInSection = [cv numberOfItemsInSection:section];
NSIndexPath *firstCellIndexPath = [NSIndexPath indexPathForItem:0 inSection:section];
NSIndexPath *lastCellIndexPath = [NSIndexPath indexPathForItem:MAX(0, (numberOfItemsInSection - 1)) inSection:section];
NSIndexPath *firstObjectIndexPath = [NSIndexPath indexPathForItem:0 inSection:section];
NSIndexPath *lastObjectIndexPath = [NSIndexPath indexPathForItem:MAX(0, (numberOfItemsInSection - 1)) inSection:section];
UICollectionViewLayoutAttributes *firstObjectAttrs;
UICollectionViewLayoutAttributes *lastObjectAttrs;
if (numberOfItemsInSection > 0)
{
firstObjectAttrs = [self layoutAttributesForItemAtIndexPath:firstObjectIndexPath];
lastObjectAttrs = [self layoutAttributesForItemAtIndexPath:lastObjectIndexPath];
}
else
{
firstObjectAttrs = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader
atIndexPath:firstObjectIndexPath];
lastObjectAttrs = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionFooter
atIndexPath:lastObjectIndexPath];
}
CGFloat headerHeight = CGRectGetHeight(layoutAttributes.frame);
CGPoint origin = layoutAttributes.frame.origin;
origin.y = MIN(
MAX(
contentOffset.y + cv.contentInset.top,
(CGRectGetMinY(firstObjectAttrs.frame) - headerHeight)
),
(CGRectGetMaxY(lastObjectAttrs.frame) - headerHeight)
);
layoutAttributes.zIndex = 1024;
layoutAttributes.frame = (CGRect) {
.origin = origin,
.size = layoutAttributes.frame.size
};
}
}
return answer;
}
更新:UICollectionViewDelegate
方法。
collectionView:viewForSupplementaryElementOfKind:atIndexPath:
未针对第 0 节执行。“Section = 1”是第一个记录的语句。
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGSize retval = CGSizeMake(50, 50);
retval.height += 35; retval.width += 35;
return retval;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(10, 15, 10, 15);
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"Section = %ld", indexPath.section);
UICollectionReusableView *reusableview = nil;
if (kind == UICollectionElementKindSectionHeader)
{
UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
UILabel *headerLabel;
headerLabel = [[UILabel alloc]initWithFrame:CGRectMake(120, 10, 100, 30)]
headerLabel.backgroundColor = [UIColor lightGrayColor];
headerLabel.textColor = [UIColor blackColor];
headerLabel.text = @"hello";
[headerView addSubview:headerLabel];
reusableview = headerView;
}
return reusableview;
}
最佳答案
第一部分的 layoutAttributes
为零的原因是
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
对于此部分返回 nil。当 header 大小为 0 时会发生这种情况。
所以,我认为您缺少委托(delegate)方法:
collectionView:layout:referenceSizeForHeaderInSection
。尝试实现它并确保它为所有部分返回非零值。
让我知道结果如何。
关于ios - Collection View 不显示第一部分的 float 标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30267104/