2017-07-06 4 views
0

私はdidSelectItemAtの助けを借りてcollectionviewcellのUIImageを変更しようとしています。最初にセルを選択したときに動作していますが、スクロールしてスクロールバックすると、元の画像に戻り、変更された画像が別のセルに移動します。UICollctionView didSelectItemAtの問題

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") 
    } 
} 

です!

答えて

2

チェックcell作成インデックスはセルの作成cellForItemAtの時にselectedRateに追加したとき

if(self.selectedIndex == indexPath.row){ 
    cell.changeImageToSelected() 
} 
else{ 
    cell.changeImageTounselected() 
} 

これはあなたの選択を持続します。

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 
    } 
+0

ありがとうございます。それは助けた。 –

1

これは、コレクションビューがセルをデキューするために発生しました。だからあなたはあなたの選択をdataModelに保持し、cellが選択されているかどうかを取得する必要があります。だからあなたのViewControllerにし、あなたのcellForItemAtメソッド呼び出しであなたのselectedIndexを持続:スクロール

関連する問題