gpt4 book ai didi

swift - UICollectionView 数据源和委托(delegate)导致应用程序终止

转载 作者:行者123 更新时间:2023-11-28 09:21:36 25 4
gpt4 key购买 nike

<分区>

我目前正在学习 swift CollectionView,我按照教程的每一步操作,但不知何故我的应用程序最终因此错误而终止

this is the error message

断开 dateSource 和委托(delegate)似乎可以阻止它崩溃,但它最终在 Collection View 中没有任何内容。任何提示将不胜感激:)

这是我的ViewController:

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

struct Item {
var title:String
var Image:String
var price:Double
}

let items:[Item] = [Item(title: "Mario Kart", Image: "Mario", price: 5), Item(title: "Car", Image: "Car", price: 6), Item(title: "Doll", Image: "Doll", price: 3)]



override func viewDidLoad() {
super.viewDidLoad()
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return items.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
cell.myImage.image = UIImage(named: items[indexPath.row].Image)
cell.myPrice.text = String(items[indexPath.row].price)
cell.myTitle.text = String(items[indexPath.row].title)
return cell
}


}

class CollectionViewCell: UICollectionViewCell {
@IBOutlet weak var myImage: UIImageView!
@IBOutlet weak var myPrice: UILabel!
@IBOutlet weak var myTitle: UILabel!
}

This is my storyboard

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