在收藏视图中设置图像和名称 快速 5.



所以我试图在集合视图中设置图像及其名称,我实现的是我设置了名称,但我无法将图像设置为该集合视图

IM 在此处设置名称和图标数组

let dataArray = ["Home", "Appointment", "Teleconsultation", "Lab", "Pharmacy", "Registration", "Careers", "Utilities","Feedback"]
var icons : [UIImage] = [UIImage(named: "appointment.png")!, UIImage(named: "telehealth.png")!, UIImage(named: "lab.png")!, UIImage(named: "pharmacy.png")!, UIImage(named: "labreports.png")!, UIImage(named: "laborder.png")!, UIImage(named: "pharmacyorder.png")!, UIImage(named: "logout.png")!]

然后我尝试使用它将其加载到我的集合视图中

我正在获取集合视图中的名称,但不获取图像 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int( -> Int { 返回 dataArray.count }

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ItemCell", for: indexPath)  as! ItemCell
cell.setData(text: self.dataArray[indexPath.row])
cell.setData(UIImageView: self.dataArray[indexPath.row])

return cell
}

当我使用它时,这一行会给出错误:调用中的参数标签不正确(有"UIImageView:",预期的"文本:"(

cell.setData(UIImageView: self.dataArray[indexPath.row])

我的项目单元格类

class ItemCell: UICollectionViewCell {
@IBOutlet weak var textLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
self.layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.1).cgColor
self.layer.shadowOffset = CGSize(width: 0.0, height: 3.0)
self.layer.shadowOpacity = 1.0
self.layer.shadowRadius = 10.0
self.layer.masksToBounds = false
self.contentView.layer.cornerRadius = 16.0
}
func setData(text: String) {
self.textLabel.text = text

}

}

这里的错误清楚地表明您的ItemCell类中没有方法setData(UIImageView:。 将一个添加到接受类型UIImageItemCell类中,并实现将图像设置为 imageView 的方法,您就可以开始了。在此之前,请确保单元格添加了UIImageView,并且已连接到ItemCell中的@IBOutlet以进行imageView。下面是一个示例:

class ItemCell: UICollectionViewCell {
@IBOutlet weak var imageView: UIImageView!
func setData(image: UIImage) {
imageView.image = image
}
}

注意:您还应该在某种程度上修改命名约定。

最新更新