gpt4 book ai didi

ios - 在 Collection View 中设置图像和名称 swift 5

转载 作者:行者123 更新时间:2023-12-01 16:05:23 24 4
gpt4 key购买 nike

所以我试图在集合 View 中设置图像和它们的名称我实现的是我设置了名称但我无法将图像设置为该集合 View

我在这里设置名称和图标数组

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")!]

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

我在集合 View 中获取名称但不是图像
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection 部分: 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])

我的 ItemCell 类
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



}


}

最佳答案

这里的错误明确指出您没有方法 setData(UIImageView:在您的 ItemCell类(class)。加一个到ItemCell接受类型 UIImage 的类并实现将图像设置为 imageView 的方法,您就可以开始了。在此之前,请确保单元格具有 UIImageView添加并连接到 @IBOutletItemCellimageView .下面是一个例子:

class ItemCell: UICollectionViewCell {
@IBOutlet weak var imageView: UIImageView!

func setData(image: UIImage) {
imageView.image = image
}
}

注意:您也应该在一定程度上修改您的命名约定。

关于ios - 在 Collection View 中设置图像和名称 swift 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62356738/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com