为什么 Collection 未显示可重用视图?

Why Collection Reusable View not shown?

我想用 header 创建 UICollection 视图。我在 mainStoryBoard 上设置了 Collection Reusable View,但是设备上没有显示任何内容。我试图搜索但无法找出它没有出现的原因。我

主要故事板

在设备上

进口UI套件

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {


    @IBOutlet weak var collectionView: UICollectionView!

    var images = ["medal1","medal2","medal3","medal4","medal5","medal6","medal7","medal8","medal9","medal10","medal1","medal2","medal3","medal14"]

    var texts = ["hi","yes","hoo","such","hi","yes","hoo","such","hi","yes","hoo","such","hi","yes"]

    override func viewDidLoad() {
        super.viewDidLoad()



        collectionView.delegate = self
        collectionView.dataSource = self

    }

    public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return images.count
    }

    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCell", for: indexPath) as! CustomCell

        cell.myImage.image = UIImage(named: images[indexPath.row])

        cell.achievementLabel.text = texts[indexPath.row]

        return cell

    }

    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }






}


import UIKit

Class 对于 collection 查看 class自定义单元格:UICollection视图单元格{

    @IBOutlet weak var myImage: UIImageView!

    @IBOutlet weak var achievementLabel: UILabel!

}

class 对于 Collection 可重用视图 进口 UI套件

class CollectionReusableView: UICollectionReusableView {

    @IBOutlet weak var reuseableVimage: UIImageView!
}


> import UIKit

class ViewController: UIViewController, UICollectionViewDelegate {


    @IBOutlet weak var collectionView: UICollectionView!

    var images = ["medal1","medal2","medal3","medal4","medal5","medal6","medal7","medal8","medal9","medal10","medal1","medal2","medal3","medal14"]

    var texts = ["hi","yes","hoo","such","hi","yes","hoo","such","hi","yes","hoo","such","hi","yes"]

    override func viewDidLoad() {
        super.viewDidLoad()



        collectionView.delegate = self


    }

    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        if kind == UICollectionElementKindSectionHeader {
            let view = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HeaderView", for: indexPath)
            // do any programmatic customization, if any, here
            return view
        }
        fatalError("Unexpected kind")








}
}

尝试实现这一点。 RayWenderlich 中有一个可能对您有用的示例:https://www.raywenderlich.com/136161/uicollectionview-tutorial-reusable-views-selection-reordering

override func collectionView(_ collectionView: UICollectionView,
                             viewForSupplementaryElementOfKind kind: String,
                             at indexPath: IndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionElementKindSectionHeader:
    let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind,
                                                                           withReuseIdentifier: "CollectionReusableView",
                                                                           for: indexPath) as! CollectionReusableView
    headerView.reuseableVimage ....
    return headerView
default:
    assert(false, "Unexpected element kind")
}
}

你必须实施 viewForSupplementaryElementOfKind:

  1. 根据需要为 UICollectionElementKindSectionHeaderUICollectionElementKindSectionFooter 实施 collectionView(_:viewForSupplementaryElementOfKind:at:)

    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        if kind == UICollectionElementKindSectionHeader {
            let view = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "CollectionReusableView", for: indexPath) as! CollectionReusableView
            // do any programmatic customization, if any, here
            return view
        }
        fatalError("Unexpected kind")
    }
    
  2. 确保 IB 中的 header 可重用视图具有

    • 适当的基础class;和
    • 合适的"reuse identifier"
  3. 在 IB 中,确保 collection 视图的 "Accessories" 在 "Section Header" 和 "Section Footer" 旁边有适当的复选标记。