Collection 视图不显示第一部分的浮动 header

Collection view does not display floating header for the first section

下面是我的自定义布局 class 的 layoutAttributesForELementsInRect 方法。对于第 0 部分,layoutAttributes 为零,因此第一部分 header 不会显示。 "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 returns 本节为零。当 header 的大小为 0 时会发生这种情况。

所以,我认为您缺少委托方法: collectionView:layout:referenceSizeForHeaderInSection。尝试实施它并确保它 returns 所有部分的非零值。

告诉我结果如何。