如何在swift的集合视图中同时生成页眉和页脚?

我试图将页眉和页脚组合在一起,但它不断崩溃,我找不到swift教程来理解它.

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

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

 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:

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")
    }
}

我希望有人能帮忙.

推荐答案

您可以使用UICollectionViewController来处理UICollectionView,并在Interface Builder中激活FooterHeader部分,然后可以使用以下方法在UICollectionView中预览添加的两个部分:

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")
    }
}

在上面的代码中,我把页脚和页眉的identifier分别设为HeaderFooter,例如,你可以随心所欲.如果你想创建一个自定义页眉或页脚,那么你需要 for each 页眉或页脚创建一个UICollectionReusableView的子类,并根据你的需要进行自定义.

您可以在Interface Builder中或在以下代码中注册自定义页脚和页眉类:

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

Swift相关问答推荐

Swift Tree实现中的弱var

在列表项内的NavigationLink中的按钮无法正常工作

Form中的Divider在macOS上并不完全扩展

什么是Swift Concurrency中任务组的正常退出

如何为任务扩展的泛型静态函数获得自动类型推理

通常从数组调用的SWIFT静态协议函数

在MacOS上检测键盘是否有Globe键

可选,类似于自定义类型的初始化

表视图插座单元测试失败

Swift iOS:确定贝塞尔曲线路径是否与 UIView 框架相交?

列表不显示信息

deinitialize() 与 deallocate()

并发执行代码中捕获的 var 的变异

如何制作具有 2 个视图的 3D 旋转动画?

如何使用带有 span 样式标签的 Swift XMLParser

如何在 Swift 中对单个单词进行词形还原

如何 for each 用户制作一个单独的按钮,以便在按下它时切换 isFollowingUser Bool 并更改为关注?

在视图中设置所有变量

swift 中的@property/@synthesize 类似功能

如何将 Int 拆分为其各个数字?