gpt4 book ai didi

ios - 从数组快速更改 UICollectionViewCell 中的 UIImage

转载 作者:行者123 更新时间:2023-11-28 11:02:29 26 4
gpt4 key购买 nike

我有一个数组:

 var categoryImages = ["image1","image2","image3","image4"]

这就是我试图改变图像的方式:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let myCell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath as IndexPath)
myCell.backgroundColor = UIColor.blue

let size = CGRect(x: 0, y: 0, width: 100, height: 100)

var imageview: UIImageView = UIImageView(frame: size)

let image: UIImage = UIImage(named: categoryImages[indexPath])
imageview.image = image
myCell.contentView.addSubview(imageview)
return myCell
}

然而,这一行:

let image: UIImage = UIImage(named: categoryImages[indexPath])

抛出这个错误:

Cannot subscript a value of type '[String]' with an index of type 'IndexPath'

有人可以帮助我:我希望将数组中的图像加载到 UICollectionViewCells 中。然而,不是作为背景图像,我希望它们作为我可以定位的图像等。

提前致谢。

最佳答案

试试这个:

let image: UIImage = UIImage(named: categoryImages[indexPath.row])

您的 categoryImages 是一个数组,因此您需要通过索引从中获取元素。

关于ios - 从数组快速更改 UICollectionViewCell 中的 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40110917/

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