gpt4 book ai didi

ios - CollectionView 中的单元格未显示

转载 作者:行者123 更新时间:2023-11-30 11:56:41 27 4
gpt4 key购买 nike

我正在尝试以编程方式创建 UICollectionView。这是我到目前为止所拥有的

我还包含了 UICollectionViewController、UICollectionViewDelegateFlowLayout

    override func viewDidLoad() {
super.viewDidLoad()
self.collectionView!.backgroundColor = UIColor(red: 153/255, green: 204/255, blue: 153/255, alpha: 1)

// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
self.collectionView!.alwaysBounceVertical = true
// Register cell classes
self.collectionView!.register(threadCell.self, forCellWithReuseIdentifier: reuseIdentifier)


// Do any additional setup after loading the view.
}

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
return 3
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)

// Configure the cell

return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.height, height: 50)
}

class threadCell: UICollectionViewCell{
override init(frame: CGRect){
super.init(frame: frame)
setupViews()
}
let userName: UILabel = {
let name = UILabel()
name.text = "testing"
name.font = UIFont.boldSystemFont(ofSize: 14)
name.translatesAutoresizingMaskIntoConstraints = false
return name

}()
required init?(coder aDecoder: NSCoder) {
fatalError()
}
func setupViews(){
backgroundColor = UIColor.blue
addSubview(userName)
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0" : userName]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0" : userName]))

}
}

结果应显示 3 个单元格,每个单元格带有测试标签 应用程序不会崩溃或显示任何错误,但单元格不会出现

最佳答案

您需要在viewDidLoad()中确认dataSourcedelegate-:

override func viewDidLoad() {
super.viewDidLoad()
self.collectionView!.backgroundColor = UIColor(red: 153/255, green: 204/255, blue: 153/255, alpha: 1)

// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
self.collectionView!.alwaysBounceVertical = true
// Register cell classes
self.collectionView!.register(threadCell.self, forCellWithReuseIdentifier: reuseIdentifier)

self.collectionView.delegate = self
self.collectionView.dataSource = self

// Do any additional setup after loading the view.
}

关于ios - CollectionView 中的单元格未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47738174/

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