【问题标题】:Multiple supplementary views in a UICollectionViewFlowLayout?UICollectionViewFlowLayout 中有多个补充视图?
【发布时间】:2016-03-10 01:36:48
【问题描述】:

我有一个使用UICollectionViewFlowLayout 子类的相当标准的网格布局。当用户点击一个单元格时,我会向下移动后续行中的单元格,以便为将出现的详细视图腾出空间。它看起来像这样:

grid mockup

在该详细视图中,我想显示另一个包含相关数据的视图,类似于 iTunes 显示 album details 的方式。现有布局具有每个部分的标题,我目前正在将详细视图放置到位,手动管理框架的位置。这在旋转和细胞四处移动时变得很棘手。

如何通过将其视为补充视图来说服布局处理细节的位置?我的控制器已正确配置为将详细信息显示为补充视图,与布局相同。

【问题讨论】:

    标签: ios objective-c uicollectionview uicollectionviewlayout


    【解决方案1】:

    解决了这个问题。概括地说,这对我有用:

    1. 创建UICollectionViewLayoutAttributes 的子类并通过覆盖layoutAttributesClass 函数将其注册到您的布局中。
    2. layoutAttributesForElementsInRect: 中使用[super layoutAttributesForElementsInRect:rect]; 检索所有标准布局属性。
    3. 将该数组复制到一个可变数组中,并为补充视图附加另一组属性,例如[attributesCopy addObject:[self layoutAttributesForSupplementaryViewOfKind:YourSupplementaryKind atIndexPath:indexPathForTappedCell]];
    4. layoutAttributesForSupplementaryViewOfKind:atIndexPath: 中获取视图的属性,例如 YourLayoutAttributes *attributes = [YourLayoutAttributes layoutAttributesForSupplementaryViewOfKind:elementKind withIndexPath:indexPath];
    5. 测试elementKind 是否与您要生成的补充视图类型匹配
    6. 使用[self layoutAttributesForItemAtIndexPath:indexPath];检索单元格的布局属性
    7. 从单元格的属性中更改框架以满足补充视图的需要
    8. 将新框架分配给补充属性

    需要注意的重要部分是您不能跳过子类化 UICollectionViewLayoutAttributes。向superUICollectionViewFlowLayout 实例)询问除标准页眉或页脚之外的任何内容的补充视图属性将返回nil。我找不到任何关于这种行为的具体文档,所以我可能是错的,但根据我的经验,解决我的问题的是子类属性。

    您的代码应如下所示:

    - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
    {
        NSArray *allAttributesInRect = [super layoutAttributesForElementsInRect:rect];
        NSMutableArray *attributes = NSMutableArray.array;
    
        for (UICollectionViewLayoutAttributes *cellAttributes in allAttributesInRect)
        {
            // Do things with regular cells and supplemental views
        }
    
        if (self.selectedCellPath)
        {
            UICollectionViewLayoutAttributes *detailAttributes = [self layoutAttributesForSupplementaryViewOfKind:SomeLayoutSupplimentaryDetailView atIndexPath:self.selectedCellPath];
            [attributes addObject:detailAttributes];
        }
    
        return attributes.copy;
    }
    
    
    - (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath
    {
        SomeLayoutAttributes *attributes = [SomeLayoutAttributes layoutAttributesForSupplementaryViewOfKind:elementKind withIndexPath:indexPath];
    
        if ([elementKind isEqualToString:SomeLayoutSupplimentaryDetailView])
        {
            UICollectionViewLayoutAttributes *cellAttributes = [self layoutAttributesForItemAtIndexPath:indexPath];
            CGRect frame = cellAttributes.frame;
    
            frame.size.width = CGRectGetWidth(self.collectionView.frame); // or whatever works for you
            attributes.frame = frame;
        }
    
        return attributes;
    }
    

    UICollectionViewLayoutAttributes 子类不一定需要任何额外的属性或函数,但它是存储特定于该视图的数据以供在使用 dequeueReusableSupplementaryViewOfKind:forIndexPath: 检索视图后使用的配置使用的好地方。

    【讨论】:

    • 嘿,我们可以谈谈吗?给我发电子邮件 kataria.pavan@gmail.com 我正在图书馆工作,可以利用您的专业知识。它与第 0 节的列标题有关。目前我只能为第 0 节调用一次补充方法,让我只有一个列标题哈哈。
    猜你喜欢
    • 1970-01-01
    • 2017-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-24
    • 2015-12-31
    • 2013-02-20
    • 2015-12-22
    相关资源
    最近更新 更多