我正在使用 swift 3 - 我知道如何使用自定义 Cell 在 Collection View 中显示一些图像 - 问题是我无法在 Collection View Controller 中使用自定义单元格
这里是 Collection View Controller 代码 private let reuseIdentifier = "uploadCell"
上传文件类:UICollectionViewController {
var images = ["1.jpg" , "2.jpg" , "3.jpg" , "4.jpg" ]
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
//myCollectionView.sizeToFit()
//cell.sizeToFit()
cell.cellImages.image = UIImage(named: images[indexPath.row])
return cell
}
这是 Collection View 单元代码
import UIKit
class UICollectionViewCell: UICollectionViewCell {
@IBOutlet weak var cellImages: UIImageView!
}
记住我使用“uploadCell”作为标识符
最佳答案
对于使用 CollectionView 自定义单元格
第 1 步:创建 CollectionViewCell 的子类
class ImageCell: UICollectionViewCell {
@IBOutlet weak var imgView: UIImageView!
}
第二步:在 StoryBoard 中设置 CollectionViewCell 类
第三步:重用CollectionView Cell
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCell", for: indexPath) as! ImageCell
cell.imgView.image = self.arrMedia[indexPath.row] as? UIImage
return cell
}
关于ios - 如何在 swift 3 的 Collection View Controller 中使用自定义单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44257423/