3

我见过的所有示例都使用补充视图作为页眉或页脚。我需要在流程布局中的每个单元格上方和/或下方添加一个标签。

起初,我认为我所要做的就是注册一个类作为补充视图,然后实现collectionViewForSupplementaryElementOfKindAtIndexPath ,我会很高兴,但开箱即用的FlowLayout似乎只支持页眉和页脚。

苹果文档似乎暗示更多需要FlowLayout的子类化工作。

由于我不想重做苹果为流布局所做的工作,并且由于我的单元格的大小预计会有所不同,所以我想扩展 Flow Layout 以支持每个单元格的 supp 视图。

我想我应该能够通过继承 UICollectionViewFlowLayout 来实现这一点。

但我不确定如何告诉流程布局为每个单元格请求补充视图。简单地为 supp 视图注册一个类不会强制布局调用layoutAttributesForSupplementaryViewOfKind。就目前而言,这从未在我的子类中调用过。我想如果我为 supp 视图注册一个类,它应该为我的部分中的每个项目请求 sup 视图……这似乎是一个不正确的假设。

我见过一个很棒的自定义布局示例,其中使用 NSDictionaries 手动管理布局,但我不确定如何将这些知识应用于内置流布局。

4

1 回答 1

6

好吧,您需要为每个需要的单元格添加描述可重用视图的属性。您可以根据已经存在的用于布置单元格的属性为每个单元格添加这些属性。您可以在 layoutAttributesForElementInRect 中执行此操作。

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    //first get a copy of all layout attributes that represent the cells. you will be modifying this collection.
    NSMutableArray *allAttributes = [[super layoutAttributesForElementsInRect:rect] mutableCopy]; 

    //go through each cell attribute
    for (UICollectionViewLayoutAttributes *attributes in [super layoutAttributesForElementsInRect:rect])
    {
        //add a title and a detail supp view for each cell attribute to your copy of all attributes
        [allAttributes addObject:[self layoutAttributesForSupplementaryViewOfKind:SomeCellDetailsKind atIndexPath:[attributes indexPath]]];
        [allAttributes addObject:[self layoutAttributesForSupplementaryViewOfKind:SomeCellTitleKind atIndexPath:[attributes indexPath]]];
    }

    //return the updated attributes list along with the layout info for the supp views
    return allAttributes;
}

-(UICollectionViewLayoutAttributes*) layoutAttributesForSupplementaryViewOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    //create a new layout attributes to represent this reusable view
    UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:kind withIndexPath:indexPath];

    if(attrs){

        //get the attributes for the related cell at this index path
        UICollectionViewLayoutAttributes *cellAttrs = [super layoutAttributesForItemAtIndexPath:indexPath];

        if(kind == SomeCellDetailsKind){
            //position this reusable view relative to the cells frame
            CGRect frame = cellAttrs.frame;
            frame.origin.y += (frame.size.height - _detailHeight);
            frame.size.height = _detailHeight;
            attrs.frame = frame;
        }

        if(kind == SomeCellTitleKind){
            //position this reusable view relative to the cells frame
            CGRect frame = cellAttrs.frame;
            frame.origin.y -= _titleHeight; //+= frame.size.height; //( - frame.size.height;
            frame.size.height = _titleHeight;
            attrs.frame = frame;
        }
    }

    return attrs;
}

然后你可以实现 collectionViewForSupplementaryElementOfKindAtIndexPath 来描述视图应该是什么样子。

就像 Derrick Hathaway 提到的那样,流布局不会考虑 supp 视图的高度来调整行高,因此请确保适当地调整集合视图上的最小行高。

于 2013-06-25T23:29:00.340 回答