ios - Collection View 不显示第一部分的 float 标题

标签 ios objective-c uicollectionviewlayout

下面是我的自定义布局类的 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/

相关文章:

ios - UICollectionView 单元格的动态高度

ios - 使用 'YouTube-Player-iOS-Helper' ,是否可以在从屏幕上移除 YTPlayerView 时不暂停播放?

ios - xcodebuild 可以管理自动签名吗?

iOS NSDateFormatter 以数字形式格式化日期和年份,以文字形式格式化日期

objective-c - NSMenuItem 自定义 View 中的可编辑 NSTextField

ios - UICollectionView 添加了多个装饰 View

ios - CollectionView 布局在滚动之前不会刷新

ios - 如何在混合代码 Objective-C/C++ 文件(.mm 文件)中使用 PHPhotoLibrary?

iphone - 无法执行“codesign”(无此文件或目录)

iphone - 在 'UITableView' 中选择一行时调用新 View