为什么不显示集合可重用视图?

时间:2017-05-16 15:13:49

标签: ios swift uicollectionview uicollectionreusableview

我想用标题创建UICollectionView。我在mainStoryBoard上设置了Collection Reusable View,但是设备上没有显示任何内容。我试图搜索,但无法找出它为什么没有出现。我

主要故事板

enter image description here

在设备上

enter image description here

导入UIKit

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 CustomCell:UICollectionViewCell {

    @IBOutlet weak var myImage: UIImageView!

    @IBOutlet weak var achievementLabel: UILabel!

}

集合可重用视图的类     导入UIKit

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








}
}

2 个答案:

答案 0 :(得分:7)

您必须实施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中的标题可重用视图

    • 适当的基类;和
    • 适当的"重用标识符"
  3. 在IB中,确保收藏视图"附件"接下来有选中标记" Section Header"和#34;部分页脚",视情况而定。

答案 1 :(得分:1)

尝试实现此目的。 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")
}
}