如何使用swift在集合视图中制作页眉和页脚 [英] How to make both header and footer in collection view with swift

查看:20
本文介绍了如何使用swift在集合视图中制作页眉和页脚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何快速在集合视图中同时制作页眉和页脚?

我正在尝试将页眉和页脚组合在一起,但它总是崩溃,我找不到快速教程来理解它.

我不知道如何返回两者的补充视图,而只是返回一个.

我将它们都设置在故事板上(类 + 标识符)

 覆盖 func numberOfSectionsInCollectionView(collectionView: UICollectionView) ->诠释{//#warning 方法实现不完整 -- 返回节数返回 2}覆盖 func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) ->诠释{//#warning 方法实现不完整 -- 返回section中的item数返回 10}覆盖 func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) ->UICollectionReusableView {var header: headerCell!var 页脚:页脚单元格!如果种类 == UICollectionElementKindSectionHeader {标题 =collectionView.dequeueReusableSupplementaryViewOfKind(种类,withReuseIdentifier: "header", forIndexPath: indexPath)作为?标题单元格}返回标题}

<块引用>

错误:标识符为 1 的 UICollectionElementKindCell - 必须为标识符注册一个 nib 或一个类,或者连接情节提要中的原型单元'

覆盖 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) ->简介CC {让 cell = collectionView.dequeueReusableCellWithReuseIdentifier("one", forIndexPath: indexPath) as!简介CC//配置单元格返回单元格}覆盖 func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) ->UICollectionReusableView {切换种类{案例 UICollectionElementKindSectionHeader:让 headerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "header", forIndexPath: indexPath) as!标题单元格headerView.backgroundColor = UIColor.blueColor();返回标题视图案例 UICollectionElementKindSectionFooter:让 footerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "footer", forIndexPath: indexPath) as!页脚单元格footerView.backgroundColor = UIColor.greenColor();返回页脚视图默认:断言(假,意外的元素种类")}}

希望有人能帮忙.

解决方案

您可以制作一个 UICollectionViewController 来处理 UICollectionView 并在 Interface Builder 中激活 FooterHeader 部分,那么您可以使用以下方法在您添加的 UICollectionView 两个部分中进行预览:

override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) ->UICollectionReusableView {切换种类{案例 UICollectionView.elementKindSectionHeader:let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Header", for: indexPath)headerView.backgroundColor = UIColor.blue返回标题视图案例 UICollectionView.elementKindSectionFooter:let footerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Footer", for: indexPath)footerView.backgroundColor = UIColor.green返回页脚视图默认:断言(假,意外的元素种类")}

在上面的代码中,我将页脚和页眉的 identifier 设置为 HeaderFooter 例如,您可以按照自己的方式进行操作想.如果要创建自定义页眉或页脚,则需要为每个子类创建 UICollectionReusableView 并根据需要对其进行自定义.

您可以通过以下方式在 Interface Builder 或代码中注册您的自定义页脚和标题类:

registerClass(myFooterViewClass, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "myFooterView")

How to make both header and footer in collection view in swift ?

I'm trying to combine a header and a footer together but it keep crashing, I couldn't find swift tutorial to understand it.

I don't how to return supplementary view for both rather just one .

I set them both on the storyboard (class + identifier )

 override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    //#warning Incomplete method implementation -- Return the number of sections
    return 2
}


override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    //#warning Incomplete method implementation -- Return the number of items in the section
    return 10
}




override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
    var header: headerCell!
    var footer: footerCell!



    if kind == UICollectionElementKindSectionHeader {
        header =
            collectionView.dequeueReusableSupplementaryViewOfKind(kind,
                withReuseIdentifier: "header", forIndexPath: indexPath)
            as? headerCell

}
    return header

}

Error: UICollectionElementKindCell with identifier one - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> profileCC {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("one", forIndexPath: indexPath) as! profileCC

    // Configure the cell

    return cell
}



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! headerCell

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

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

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

    default:

        assert(false, "Unexpected element kind")
    }
}

I hope someone will help.

解决方案

You can make an UICollectionViewController to handle the UICollectionView and in Interface Builder activate the Footer and Header sections, then you can use the following method for preview in you UICollectionView the two sections added :

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

    switch kind {

    case UICollectionView.elementKindSectionHeader:

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

        headerView.backgroundColor = UIColor.blue
        return headerView

    case UICollectionView.elementKindSectionFooter:
        let footerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Footer", for: indexPath)

        footerView.backgroundColor = UIColor.green
        return footerView

    default:

        assert(false, "Unexpected element kind")
    }

In the above code I put the identifier for the footer and header as Header and Footer for example, you can do it as you want. If you want to create a custom header or footer then you need to create a subclass of UICollectionReusableView for each and customize it as you want.

You can register your custom footer and header classes in Interface Builder or in code in the following way:

registerClass(myFooterViewClass, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "myFooterView")

这篇关于如何使用swift在集合视图中制作页眉和页脚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆