问题描述
class FinalImageViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {
var not: [UIImage?] = [#imageLiteral(resourceName: "silly5"),#imageLiteral(resourceName: "special16")]
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 3
}
func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
return not.count
}
func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CVImageView",for: indexPath) as! CVCell
cell.cellImageView.image = not[indexPath.row]
return cell
}
@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
print(ImagesOnClick)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(CVCell.self,forCellWithReuseIdentifier: "CVImageView")
}
}
还有我的手机:
class CVCell: UICollectionViewCell {
@IBOutlet weak var cellImageView: UIImageView!
}
然后在情节提要中,将imageView标记设置为3,将单元格标识符设置为CVImageView
,但是当我运行项目时,集合视图只是全白的,没有任何项目显示。顺便说一句,如果您想知道ImagesOnClick
是什么,它只是用户选择的图像的数组。
解决方法
您可以在情节提要中找到您的班级名称,它应该像这样
class FinalImageViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
return ImagesOnClick.count
}
func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withIdentifier: "CVImageView",for: indexPath) as! CVImageCell //replace CVImageCell with your cell class name
cell.imageView.image = ImagesOnClick[indexPath.row] //replace imageView with your image view name with tag=3
return cell
}
@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
print(ImagesOnClick)
collectionView.register(CVImageCell.self,forCellWithReuseIdentifier: "CVImageView") //replace CVImageCell with your cell class name
collectionView.delegate = self
collectionView.dataSource = self
}
}