UICollctionView didSelectItemAt 问题
UICollctionView didSelectItemAt problems
我试图在 didSelectItemAt 的帮助下更改 collectionviewcell 中的 UIImage。当我 select 单元格开始时它正在工作,但是当我向下滚动并向上滚动时,图像恢复到原来的状态并且更改后的图像转移到不同的单元格。
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
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")
}
}
请帮忙!!
发生这种情况是因为集合视图使他的单元格出队。因此,您需要将您的选择保存在 dataModel 中,并获取是否选择了单元格。因此,请将您的 selectedIndex 保存在您的 viewController 和您的 cellForItemAt 方法调用中:
if(self.selectedIndex == indexPath.row){
cell.changeImageToSelected()
}
else{
cell.changeImageTounselected()
}
这将在滚动时保留您的选择
检查单元格创建时 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
}
我试图在 didSelectItemAt 的帮助下更改 collectionviewcell 中的 UIImage。当我 select 单元格开始时它正在工作,但是当我向下滚动并向上滚动时,图像恢复到原来的状态并且更改后的图像转移到不同的单元格。
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
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")
}
}
请帮忙!!
发生这种情况是因为集合视图使他的单元格出队。因此,您需要将您的选择保存在 dataModel 中,并获取是否选择了单元格。因此,请将您的 selectedIndex 保存在您的 viewController 和您的 cellForItemAt 方法调用中:
if(self.selectedIndex == indexPath.row){
cell.changeImageToSelected()
}
else{
cell.changeImageTounselected()
}
这将在滚动时保留您的选择
检查单元格创建时 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
}