如何解决collectionViewCell:展开可选值
我创建了一个收集视图,我希望该单元格显示两个标签(“产品”和“价格”)。标签的标题将基于数组(产品列表)。
因此,我有一个UIViewController类(其中是我的集合视图代码)和一个UICollectionViewCell类(其中是具有此代码的函数):
class ShoppingCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var produitLabel: UILabel!
@IBOutlet weak var prixLabel: UILabel!
func manageLabel(with produitName: String) {
produitLabel.text = produitName
}
}
在我的CollectionViewController中,这是我的代码:
class ShoppingViewController: UIViewController {
// IBoutlet
@IBOutlet weak var collectionView: UICollectionView!
// IB action
//
var typeOfProducts = ProduitsStr.init(products: ["bananes","pommes"])
override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
}
}
extension ShoppingViewController: UICollectionViewDelegate {
}
extension ShoppingViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
return typeOfProducts.products.count
}
func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = ShoppingCollectionViewCell()
if let produitCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell",for: indexPath) as? ShoppingCollectionViewCell {
produitCell.manageLabel(with: typeOfProducts.products[indexPath.row])
}
return cell
}
}
问题是,当我运行该应用程序时,出现此错误:
Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value: file /Users/_/Desktop/developppemnt application/Project FoodApp_v4/Project FoodApp_v4/controller/ShoppingCollectionViewCell.swift,line 23 2020-10-09 17:26:56.600385+0200 Project FoodApp_v4[18353:106284] Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value: file /Users/_/Desktop/developppemnt application/Project FoodApp_v4/Project FoodApp_v4/controller/ShoppingCollectionViewCell.swift,line 23
我不明白为什么会遇到这个问题,有人可以帮我吗?
谢谢!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。