UICollectionViewFlowLayout中有多个补充视图?

时间:2016-03-10 01:36:49

标签: ios objective-c uicollectionview uicollectionviewlayout

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

grid mockup

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

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

1 个答案:

答案 0 :(得分:2)

解决了这个问题。从广义上讲,这对我有用:

  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. 从单元格属性更改框架以满足补充视图的需要
  9. 将新框架分配给补充属性
  10. 需要注意的重要部分是您不能跳过子类化 UICollectionViewLayoutAttributes。向super(一个UICollectionViewFlowLayout实例)询问除标准页眉或页脚之外的任何内容的补充视图的属性将返回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:检索视图后,它是存储特定于该视图的数据以供配置使用的好地方。