Swift UICollectionView不显示图像
我试图在UICollectionView的帮助下显示一组水平滚动的图像。我为单元格创建了自定义视图控制器文件,但仍然没有显示任何图像。这是我在Viewcontroller中的代码:Swift UICollectionView不显示图像,swift,xcode,uiviewcontroller,uicollectionview,uicollectionviewcell,Swift,Xcode,Uiviewcontroller,Uicollectionview,Uicollectionviewcell,我试图在UICollectionView的帮助下显示一组水平滚动的图像。我为单元格创建了自定义视图控制器文件,但仍然没有显示任何图像。这是我在Viewcontroller中的代码: class ViewController: UIViewController , UICollectionViewDelegate , UICollectionViewDataSource{ var imageArray = [UIImage(named:"1"),UIImage(named:"2"),UIImag
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类中,我将图像视图链接到该类:
import UIKit
class coffeeImageCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var Imgimage: UIImageView!
}
您需要在
viewDidLoad
self.collectionView.delegate = self
self.collectionView.dataSource = self
创建这个出口
@IBOutlet weak var collectionView: UICollectionView!
我解决了。我使用的教程是两年前的,这个人正在使用版本3。我刚刚将“as”之后的“!”改为“?”,并在单元格(.image)后面添加了一个“?”。另外,我在返回单元格后添加了一个“!”来展开我遇到了两个问题:首先,我无法创建插座,因为某些原因,当我尝试时,没有显示创建插座的选项。其次,我在添加self.collectionView:对成员“collectionView(uU2;:numberOfItemsInSection:)的不明确引用时遇到此错误