gpt4 book ai didi

ios - UICollctionView didSelectItemAt 问题

转载 作者:行者123 更新时间:2023-11-28 10:53:32 24 4
gpt4 key购买 nike

我试图在 didSelectItemAt 的帮助下更改 collectionviewcell 中的 UIImage。当我在开始时选择单元格时它工作正常,但是当我向下滚动并向上滚动时,图像恢复到原来的状态并且更改后的图像转移到不同的单元格。

First cell selected Image

Scrolled down then came up Image

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collection_cell", for: indexPath) as! RateCardCollectionViewCell
let tempString = "\(NSLocalizedString("base_url", comment: ""))\(itemImageLink[indexPath.row])"
let urlnew:String = tempString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!
let url = URL(string: urlnew)
cell.rateCardImage.kf.setImage(with: url,placeholder: UIImage(named: "Junkart_Bin.png"))
cell.label.text = itemName[indexPath.row]
cell.itemRate.text = itemRate[indexPath.row]
cell.layer.cornerRadius = 3
cell.card.backgroundColor = UIColor.white
cell.card.layer.masksToBounds = false
cell.card.layer.shadowColor = UIColor.black.withAlphaComponent(0.2).cgColor
cell.card.layer.shadowOffset = CGSize(width: 0, height: 0)
cell.card.layer.shadowOpacity = 1 //0.8
return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath) as! RateCardCollectionViewCell
if self.selectedRate == nil {
cell.changeImagetoSelected()
self.selectedRate = [Int]()
self.selectedRate.append(indexPath.row)
}
else {

}
}

以下是 UICollectionViewCell 类

class RateCardCollectionViewCell: UICollectionViewCell {


@IBOutlet weak var label: UILabel!
@IBOutlet weak var rateCardImage: UIImageView!
@IBOutlet weak var card: UIView!
@IBOutlet weak var itemRate: UILabel!
@IBOutlet weak var selected_rate_image: UIImageView!
func changeImagetounSelected() {
selected_rate_image.image = UIImage(named: "unselected_rate")
}
func changeImagetoSelected() {
selected_rate_image.image = UIImage(named: "selected_rate")
}
}

请帮忙!!

最佳答案

检查单元格创建时添加到selectedRate上的cell创建索引cellForItemAt

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collection_cell", for: indexPath) as! RateCardCollectionViewCell
// Your remaining codes are here.

if selectedRate.contains(IndexPath.row) {
cell.changeImageToSelected()
} else {
cell.changeImageTounselected()
}
return cell
}

关于ios - UICollctionView didSelectItemAt 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44942656/

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