UICollectionView页眉和页脚视图

Jay*_*iyk 5 uiview ios uicollectionview swift swift2

您好我想知道我们是如何实现这一目标的?

在UITableView中我们可以简单地做tableView.tableHeaderView = SomeView;tableView.tableFooterView = SomeView;

但我想知道如何为UICollectionView做同样的事情.

PS:不是页眉和页脚

ikb*_*bal 7

试试吧...

ViewDidLoad中的第一个注册类

registerClass(myFooterViewClass, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "Footer")
Run Code Online (Sandbox Code Playgroud)

然后使用这种方法

override func collectionView(collectionView: UICollectionView,   viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {

  switch kind {

    case UICollectionElementKindSectionHeader:

        let header = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "Header", forIndexPath: indexPath) as! UICollectionReusableView

        header = SomeView
        return header

    case UICollectionElementKindSectionFooter:
        let footer = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "Footer", forIndexPath: indexPath) as! UICollectionReusableView

        footer= SomeView 
        return footer

    default:

       print("anything")
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望它有所帮助......

  • 问题是Not Section Header and Footer,这个答案是关于SectionHeader/Footer的. (8认同)

Lud*_*dry 5

UITableView具有全局标头tableHeaderViewtableFooterView,但UICollectionView没有全局标头,因此您必须使用节标头。

  • 这个答案应该是公认的。 (2认同)