gpt4 book ai didi

ios - 为什么未显示 Collection Reusable View?

转载 作者:可可西里 更新时间:2023-10-31 23:57:39 25 4
gpt4 key购买 nike

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

主要 Storyboard

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

Collection View 类 CustomCell 类:UICollectionViewCell {

    @IBOutlet weak var myImage: UIImageView!

@IBOutlet weak var achievementLabel: UILabel!

}

集合可重用 View 类 导入 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")








}
}

最佳答案

您必须实现 viewForSupplementaryElementOfKind:

  1. 实现 collectionView(_:viewForSupplementaryElementOfKind:at:) ,适用于 UICollectionElementKindSectionHeaderUICollectionElementKindSectionFooter

    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 中的标题可重用 View 有

    • 适当的基类;和
    • 适当的“重用标识符”
  3. 在 IB 中,确保 Collection View 的“附件”在“节页眉”和“节页脚”旁边有适当的复选标记。

关于ios - 为什么未显示 Collection Reusable View?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44005325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com