【问题标题】:Unable to add footer to Collection View无法将页脚添加到集合视图
【发布时间】:2018-10-16 12:35:13
【问题描述】:

我只需要一个高度为 50 且背景颜色为黑色的页脚。而已。暂时忘记下面的实现,然后告诉我你将如何实现它。

我正在使用以下内容:

 func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let view = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "FooterView", for: indexPath as IndexPath)
        // configure footer view
        return view
    }

 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize
    {
        return CGSize(width: 375, height: 100)
    }

但是,没有页脚附加到集合视图。我无法理解为什么这不起作用以及我应该如何纠正这个问题。请帮我纠正这个问题。

【问题讨论】:

  • 您能附上尺寸检查器的屏幕截图,以及页脚视图和页眉视图的高度吗?
  • 你设置delegateUICollectionView了吗?
  • @hardikparmar,请检查编辑。 @dani,是的
  • 这是自定义页脚吗?意思是,您是否为 UICollectionReusableView 使用自定义类?
  • @Dani,没有。请帮我放置一个高度为 50 且背景颜色为黑色的空页脚。就是这样。谢谢。

标签: ios swift xcode uicollectionview uicollectionviewcell


【解决方案1】:

这是我的方式。我对此进行了测试,它可以工作。

class ViewController: UIViewController, UICollectionViewDataSource {

     @IBOutlet weak var collectionView: UICollectionView!

     override func viewDidLoad() {
         super.viewDidLoad()
         collectionView.dataSource = self
     }

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

         let view = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "Footer", for: indexPath)
         view.backgroundColor = .red
         return view
     }
}

【讨论】:

  • 我得到:类型“UICollectionView”没有成员“elementKindSectionFooter”。我正在使用 Swift 4.1
  • 我正在使用 Swift 4.2 并且 UICollectionElementKindSectionFooter 已重命名。但是使用UICollectionElementKindSectionFooter 看看是否可行
【解决方案2】:

我是如何做到的:

1) 创建自定义页脚类:

import UIKit

class BlackFooterView: UICollectionReusableView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .black
    }

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

2) 在UICollectionView注册Footer类并设置参考大小(通常在需要ViewControllerviewDidLoad方法中:

override func viewDidLoad() {
    super.viewDidLoad()

    /// Class registration
    self.collectionView!.register(BlackFooterView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: footerReuseIdentifier)
    /// Reference size
    (self.collectionView.collectionViewLayout as! UICollectionViewFlowLayout).footerReferenceSize = CGSize(width: collectionView.bounds.width, height: 50)
}

3) 实现委托方法:

override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    if kind == UICollectionView.elementKindSectionFooter {
        return collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: footerReuseIdentifier, for: indexPath)
    }
    /// Normally should never get here
    return UICollectionReusableView()
}

4) 瞧:


Swift 4.2 中提供的示例

注意:

  1. 不包括细胞生成代码。
  2. 我使用UICollectionViewController 类作为示例,这就是为什么delegate 方法从override 开始
  3. 您还可以从 XIB 创建页脚。在这种情况下,应使用registerNib:forSupplementaryViewOfKind:withReuseIdentifier: 方法进行注册。

【讨论】:

    【解决方案3】:
    override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        if (kind == UICollectionElementKindSectionFooter) {
            let footerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "CartFooterCollectionReusableView", for: indexPath)
            // Customize footerView here
            return footerView
        } else if (kind == UICollectionElementKindSectionHeader) {
            let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "CartHeaderCollectionReusableView", for: indexPath)
            // Customize headerView here
            return headerView
        }
        fatalError()
    }
    

    【讨论】:

    • 请提供更多信息并使用代码括号。
    猜你喜欢
    • 2018-03-25
    • 2011-03-25
    • 2015-10-09
    • 2015-09-23
    • 2016-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多