【问题标题】:Why Collection Reusable View not shown?为什么未显示集合可重用视图?
【发布时间】:2017-05-16 15:13:49
【问题描述】:

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

主要故事板

在设备上

导入 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

集合视图类 类 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")








}
}

【问题讨论】:

    标签: ios swift uicollectionview uicollectionreusableview


    【解决方案1】:

    你必须实现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”和“Section Footer”旁边有复选标记。

    【讨论】:

    • 谢谢。我更新了代码,但现在屏幕什么也没显示。我检查了 IB 和附件,一切都很好。
    • 假设没有警告,我可能会检查视图调试器,看看集合视图中是否有视图以及它们的框架是什么。如果真的没有视图,那么我会在 UICollectionViewDataSource 方法中设置断点并查看它们是否被调用以及它们返回的值。
    • 当我设置 collectionView.dataSource = self 时,它会迷恋。
    • 这是断点 ""let view = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "CollectionReusableView", for: indexPath) as! CollectionReusableView""
    • 听起来像是标题视图没有匹配的“重用标识符”或标题的基类尚未设置。
    【解决方案2】:

    尝试实现这一点。 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")
    }
    }
    

    【讨论】:

    • 看来swift 3有一些变化:/
    猜你喜欢
    • 2020-05-13
    • 1970-01-01
    • 2021-10-03
    • 2020-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多