gpt4 book ai didi

ios - 如何将特定图像设置到特定数组位置?

转载 作者:搜寻专家 更新时间:2023-10-31 22:24:35 24 4
gpt4 key购买 nike

我正在使用 UICollectionView 进行水平滚动,在 array 中有六个图像。我想要的是当索引路径 x(1) 上的项目被单击时,图像数组将设置在除位置 1 之外的剩余项目 (0,2,3,4,5) 上。我们可以在特定位置设置特定图像吗数组的位置如果是那么如何?

Android的情况下,就像

if (selectedPosition < 0) {
viewHolder.imageView.setImageResource(coloredSmiley[position]);
} else {
viewHolder.imageView.setImageResource(selectedPosition == position ? coloredSmiley[position] : greySmiley[position]);
}


import UIKit

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

var selectedImageButton = -1
var imagearray = ["AngrysmIcon1", "UpsetsmIcon2", "ConfusedsmIcon3", "MehsmIcon4", "CurioussmIcon5" , "HappysmIcon6"]
var bwimagearray = ["AngrysmIcon1Inactive", "UpsetsmIcon2Inactive", "ConfusedsmIcon3Inactive", "MehsmIcon4Inactive", "CurioussmIcon5Inactive", "HappysmIcon6Inactive"]

var positiontag = ["0", "1", "2", "3", "4", "5"]

override func viewDidLoad() {
super.viewDidLoad()

}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

return self.positiontag.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! UIcollectionViewCellCollectionViewCell
cell.imagev.image = UIImage(named: imagearray[indexPath.row])
return cell

}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}

internal func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

selectedImageButton = indexPath.row

let cell = collectionView.cellForItem(at: indexPath) as! UIcollectionViewCellCollectionViewCell
if selectedImageButton < 0 {

//cell.imagev.image = bwimagearray[indexPath.row]

} else {
cell.imagev.image = UIImage(named: imagearray[indexPath.row])

}
}
}

selectedposition 是全局的,值为 -1

最佳答案

Android 片段,我相信你需要改变 cellForRowAt 如下,

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! UIcollectionViewCellCollectionViewCell
let imageName: String
if selectedImageButton < 0 {
imageName = imagearray[indexPath.row]
} else {
imageName = selectedImageButton == indexPath.row ? imagearray[indexPath.row] : bwimagearray[indexPath.row]
}
cell.imagev.image = UIImage(named: imageName)
return cell

}

然后在didSelectItemAt中设置selectedImageButton并重新加载collectionView,

internal func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)   {
selectedImageButton = indexPath.row
collectionView.reloadData()
}

注意:ReloadData 可能不被推荐,您应该寻找一些更具 react 性的方式来通知单元格更新图像。

关于ios - 如何将特定图像设置到特定数组位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53057525/

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