【发布时间】:2020-09-23 14:37:03
【问题描述】:
我有以下代码:
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 是什么,它只是用户选择的图像的数组。
【问题讨论】:
-
可能需要加:
func numberOfSections(in collectionView: UICollectionView) -> Int { return 1 } -
我只是尝试添加它并没有工作:(
-
ImagesOnClick在哪里填充?顺便说一句,viewWithTag已经过时了很长时间。设计一个带有插座的自定义单元(UICollectionViewCell子类)。 -
如果您查看我的问题,我已经通过创建
UICollectionViewCell class更改了代码我也更改了ImageOnClicknot(不是两个图像文字的数组),仅用于测试目的但仍然得到相同的结果。
标签: swift xcode uicollectionview uicollectionviewcell