我有一个数组:斯威夫特变化的UIImage从Array
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中。不过,作为背景图像,我想他们作为我可以定位的图像等。
在此先感谢。
'IndexPath'不是'Int',您可以使用它从索引获取值。 'IndexPath'可以看作是'Int'链。你需要使用'section' /'item' /'row'来获得适当的索引(这是你应该检查自己的一个套件,它取决于你的收藏视图的细节)。 – user28434