UICollectionView 不显示图像

UICollectionView does not display the images

我试图在 UICollectionView 的帮助下显示一组可水平滚动的图像。我为单元格做了一个自定义视图控制器文件,但我仍然没有显示任何图像。这是我在 Viewcontroller:

中的代码
class ViewController: UIViewController , UICollectionViewDelegate , UICollectionViewDataSource{

var imageArray = [UIImage(named:"1"),UIImage(named:"2"),UIImage(named:"3"),UIImage(named:"4"),UIImage(named:"5"),UIImage(named:"6"),UIImage(named:"7"),UIImage(named:"8"),]

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
}

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "coffeeImageCollectionViewCell", for: indexPath)   as! coffeeImageCollectionViewCell
    cell.Imgimage.image = imageArray[indexPath.row]

    return cell
  }
}

这是在我的自定义 CollectionViewCell class 中,我将图像视图链接到它:

import UIKit

 class coffeeImageCollectionViewCell: UICollectionViewCell {

@IBOutlet weak var Imgimage: UIImageView!
}

您需要在viewDidLoad

中设置
self.collectionView.delegate = self
self.collectionView.dataSource = self

创建这个出口

@IBOutlet weak var collectionView: UICollectionView!

我解决了。我使用的教程是 2 年前的,这个人使用的是版本 3。我只是更改了“!”在 "as" 到“?”之后并添加了一个“?”在单元格 (.image) 之后。另外,我添加了一个“!”在 return 单元格后展开