UIColctionView didSelectItemAt 问题

Posted

技术标签:

【中文标题】UIColctionView didSelectItemAt 问题【英文标题】:UICollctionView didSelectItemAt problems 【发布时间】:2017-07-06 07:42:06 【问题描述】:

我正在尝试在 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")
    

请帮忙!!

【问题讨论】:

【参考方案1】:

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

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
    

【讨论】:

【参考方案2】:

发生这种情况是因为集合视图使他的单元格出列。因此,您需要将您的选择保留在 dataModel 中,并从中获取是否选择了单元格。因此,将您的 selectedIndex 保留在您的 viewController 和您的 cellForItemAt 方法调用中:

if(self.selectedIndex == indexPath.row)
    cell.changeImageToSelected()

else
    cell.changeImageTounselected()

这将在滚动时保留您的选择

【讨论】:

以上是关于UIColctionView didSelectItemAt 问题的主要内容,如果未能解决你的问题,请参考以下文章

响应者链但不是委托属性将值从容器传递回视图控制器