gpt4 book ai didi

ios - Swift iOS CollectionView xib 属性为零

转载 作者:行者123 更新时间:2023-11-28 07:49:06 25 4
gpt4 key购买 nike

浏览单元格.swift

import UIKit

class BrowseCell: UICollectionViewCell {

@IBOutlet weak var image: UIImageView!

@IBOutlet weak var overlayView: UIImageView!

}

CustomCollectionViewController.swift

override func viewDidLoad() {
// collectionView?.register(BrowseCell.self, forCellWithReuseIdentifier: "browse")
collectionView?.register(UINib(nibName: "BrowseCell", bundle: nil), forCellWithReuseIdentifier: "browse")

collectionView?.dataSource = self
collectionView?.delegate = self
}

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "browse", for: indexPath) as! BrowseCell
cell.image = UIImageView(image: UIImage(named: "6"))
cell.overlayView = UIImageView(image: UIImage(named: "6"))
let test = UIImageView(image: UIImage(named: "6"))
cell.backgroundColor = UIColor.blue
return cell
}

BrowseCell.xib

单元格显示为蓝色,所以我知道 collectionView 正在工作。nibName 是正确的,重用标识符有效。调试时它说 cell.image 为零。我尝试了很多不同的组合,但不确定如何继续。谢谢

最佳答案

问题是在 imageView.image 中分配

 override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "browse", for: indexPath) as! BrowseCell

/// Here you can try making a change
/// You are assigning imageView.image a new ImageView which Is wrong
cell.image = UIImageView(image: UIImage(named: "6"))

/// Replace above line as
cell.image.image = UIImage.init(named: "6")

cell.overlayView = UIImageView(image: UIImage(named: "6"))
let test = UIImageView(image: UIImage(named: "6"))
cell.backgroundColor = UIColor.blue
return cell
}

关于ios - Swift iOS CollectionView xib 属性为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50143339/

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