首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Swift中添加UICollectionView的页眉和页脚视图

如何在Swift中添加UICollectionView的页眉和页脚视图
EN

Stack Overflow用户
提问于 2015-12-15 13:59:02
回答 4查看 18.2K关注 0票数 6

我已经在控制器类中添加了委托函数,但它不会在运行时调用。

代码语言:javascript
复制
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
}
EN

回答 4

Stack Overflow用户

发布于 2015-12-15 14:45:22

代码语言:javascript
复制
override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {

    switch kind {

    case UICollectionElementKindSectionHeader:

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

        headerView.backgroundColor = UIColor.blueColor();
        return headerView

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

        footerView.backgroundColor = UIColor.greenColor();
        return footerView

    default:

        assert(false, "Unexpected element kind")
    }
}
票数 9
EN

Stack Overflow用户

发布于 2018-01-11 16:12:21

我遵循本教程的Appcoda tutorial并更新了swift 4.0的代码

注册类:

代码语言:javascript
复制
self.collectionView.register(HeadView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HeadView")

代表们:

代码语言:javascript
复制
// For header size (required)
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    return CGSize(width:collectionView.frame.size.width, height:50.0)
}


func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

    switch kind {

    case UICollectionElementKindSectionHeader:

        let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HeadView", for: indexPath)

        headerView.backgroundColor = UIColor.blue;
        return headerView

    default:

        fatalError("Unexpected element kind")
    }
}
票数 7
EN

Stack Overflow用户

发布于 2016-11-24 17:01:57

未调用委托方法,因为您需要为补充视图注册一个类;

代码语言:javascript
复制
yourCollectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "collectionHeaderID")

yourCollectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "collectionFooterID")
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34282085

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档