16

我有一个集合视图,我希望每个部分都有页眉和页脚。我正在使用默认的流布局。

我有自己的子类,我在视图控制器UICollectionReusableView的方法中为页眉和页脚注册了每个子类。viewDidLoad

我已经实现了该方法collectionView:viewForSupplementaryElementOfKind:atIndexPath:,但是对于每个部分,它只被称为kindbeing UICollectionElementKindSectionHeader。因此我的页脚甚至没有被创建。

任何想法为什么会发生这种情况?

4

3 回答 3

12

看来我必须footerReferenceSize为集合视图布局设置。奇怪的是我不必对标题这样做。

于 2012-11-02T08:43:13.990 回答
9

(使用 Swift 3.1、Xcode 8.3.3)
首先,注册 header 的类或 nib

collectionView.register(ShortVideoListHeader.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "header")

、设置headerReferenceSize;或者,您可以headerReferenceSize在 collectionView 的委托中返回

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    (collectionView.collectionViewLayout as? UICollectionViewFlowLayout)?.headerReferenceSize = CGSize(width: view.bounds.width, height: 156)
}

第三,编写自己的头类,例如,

class ShortVideoListHeader: UICollectionReusableView {
    let titleLabel = UILabel()

    override init(frame: CGRect) {
        super.init(frame: frame)

        addSubview(titleLabel)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        titleLabel.sizeToFit()
        titleLabel.frame.origin = CGPoint(x: 15, y: 64 + (frame.height - 64 - titleLabel.frame.height) / 2) // navigationBar's height is 64
    }
}

第四,在collectionView的dataSource方法中返回你的header实例,

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    switch kind {
    case UICollectionElementKindSectionHeader:
        let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "header", for: indexPath) as! ShortVideoListHeader
        header.titleLabel.text = title
        header.setNeedsLayout()
        return header

    default:
        return UICollectionReusableView()
    }
}

顺便说一句,Apple 的 Document回答了如何隐藏节标题。

此方法必须始终返回有效的视图对象。如果您在特定情况下不想要补充视图,则您的布局对象不应为该视图创建属性。或者,您可以通过将相应属性的 hidden 属性设置为 YES 或将属性的 alpha 属性设置为 0 来隐藏视图。要在流布局中隐藏页眉和页脚视图,您还可以设置这些视图的宽度和高度为 0。

于 2017-09-11T08:40:00.007 回答
3

我发现了一些代码也许可以帮助你

- ( UICollectionReusableView * ) collectionView : ( UICollectionView * ) collectionView viewForSupplementaryElementOfKind : ( NSString * ) kind atIndexPath : ( NSIndexPath * ) indexPath
{
    UICollectionReusableView * reusableview = nil ;

    if ( kind == UICollectionElementKindSectionHeader )
    {
        RecipeCollectionHeaderView * headerView = [ collectionView dequeueReusableSupplementaryViewOfKind : UICollectionElementKindSectionHeader withReuseIdentifier : @ "HeaderView" forIndexPath : indexPath ] ;
        NSString * title = [ [ NSString alloc ] initWithFormat : @ "Recipe Group #%i" , indexPath.section + 1 ] ;
        headerView.title.text = title;
        UIImage * headerImage = [ UIImage imageNamed : @ "header_banner.png" ] ;
        headerView.backgroundImage.image = headerImage;

        reusableview = headerView;
    }
    if ( kind == UICollectionElementKindSectionFooter )
    {
        UICollectionReusableView * footerview = [ collectionView dequeueReusableSupplementaryViewOfKind : UICollectionElementKindSectionFooter withReuseIdentifier : @ "FooterView" forIndexPath : indexPath ] ;

        reusableview = footerview;
    }

    return reusableview;
}
于 2013-10-16T02:27:02.450 回答