我使用UICollectionViewFlowLayout
的子类获得了相当标准的网格布局。当用户点击一个单元格时,我会将后续行中的单元格向下移动,以便为将出现的详细视图腾出空间。它看起来像这样:
在该详细视图中,我想显示另一个包含相关数据的视图,类似于iTunes显示album details的方式。现有的布局包含每个部分的标题,我目前正在将细节视图打到位,手动管理框架的位置。旋转和细胞四处移动会变得棘手。
如何通过将布局视为补充视图来说服布局处理细节的位置?我的控制器配置正确,可以将细节显示为补充视图,与布局相同。
答案 0 :(得分:2)
解决了这个问题。从广义上讲,这对我有用:
UICollectionViewLayoutAttributes
的子类,并通过覆盖layoutAttributesClass
函数在布局中注册它。 layoutAttributesForElementsInRect:
中使用[super layoutAttributesForElementsInRect:rect];
检索所有标准布局属性。[attributesCopy addObject:[self layoutAttributesForSupplementaryViewOfKind:YourSupplementaryKind atIndexPath:indexPathForTappedCell]];
layoutAttributesForSupplementaryViewOfKind:atIndexPath:
中获取视图的属性
YourLayoutAttributes *attributes = [YourLayoutAttributes layoutAttributesForSupplementaryViewOfKind:elementKind withIndexPath:indexPath];
elementKind
是否与您要生成的补充视图的类型相匹配[self layoutAttributesForItemAtIndexPath:indexPath];
需要注意的重要部分是您不能跳过子类化 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:
检索视图后,它是存储特定于该视图的数据以供配置使用的好地方。