2017-08-16 14 views
1

私は現在CollectionViewに再利用可能なセルが1つあり、ImageViewで埋め尽くされています。この細胞は10回使用され、10種類の動物が表示されます(私は細胞の画像を提供します)。このコレクションビューの上にラベルがあります。ユーザがセルを押すたびに、ラベルは "動物の名前が選択されました"に変わります。 私が今したいのは、選択されたときに細胞イメージが暗くなり、選択されていないときに正常に戻ることです。現時点では、暗いもので手動で切り替えることで、セルの画像を暗くする方法を知っていますが、別のものを選択すると元の状態に戻す方法はわかりません。 これはどのように実装しますか?私は一番下に私のコードをコピーしている:これを行うためにはCollectionViewの選択に基づいてImageViewを変更する

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

     //define the cell 
     let cell = collectionview.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! AnimalSelectionCell 

     //animals is name of array of different types of animals 
     cell.animalImage.image = UIImage(named: animals[indexPath.row]) 
     cell.animalImage.restorationIdentifier = animals[indexPath.row] 

     return cell 
    } 

    //choosing an animal 
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {   
      let currentCell = collectionview.cellForItem(at: indexPath) as! AnimalSelectionCell 
      let animal = currentCell.animalImage.restorationIdentifier! //gets what type of animal selected 
      selectionLabel.text = "\(animal) selected" //changes label 
      currentCell.animalImage.image = UIImage(named: animal + "Selected") //changes to darker animal image     
     } 

答えて

1

あなたはselectedIndexpathと呼ばれるvarを定義し、あなたの中にすることができますdidSelectItemAt selected indexpathによるメソッドの変更、selectedIndexPathが同じ場合はselectedIndexpathをnilにし、cellForItemAtメソッドでは、現在のindexPathがselectedIndexpathプロパティと等しいかどうかを確認するだけです

var selectedIndexPath : IndexPath? //declare this 

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

     //define the cell 
     let cell = collectionview.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! AnimalSelectionCell 

     //animals is name of array of different types of animals 
     cell.animalImage.image = UIImage(named: animals[indexPath.row]) 
     if(indexPath == selectedIndexPath){ 
      //if indexpath is selected use your darker image 
      cell.animalImage.image = UIImage(named: animal + "Selected") 
     } 
     cell.animalImage.restorationIdentifier = animals[indexPath.row] 
     return cell 
    } 

    //choosing an animal 
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {   
      let currentCell = collectionview.cellForItem(at: indexPath) as! AnimalSelectionCell 
      let animal = currentCell.animalImage.restorationIdentifier! //gets what type of animal selected 
      selectionLabel.text = "\(animal) selected" //changes label 
      var prevSelectedIndexPath : IndexPath? 
      if(selectedIndexPath != nil){ 
       if(selectedIndexPath == indexPath){ 
        selectedIndexPath = nil 
       }else{ 
        prevSelectedIndexPath = IndexPath(row: selectedIndexPath.row, section: selectedIndexPath.section) 
        selectedIndexPath = indexPath 
       } 
      }else{ 
       selectedIndexPath = indexPath 
      } 

      if(prevSelectedIndexPath != nil){ 
       collectionView.reloadItems(at: [indexPath,prevSelectedIndexPath]) 
      }else{ 
       collectionView.reloadItems(at: [indexPath]) 
      } 
     } 

func clearSelection(){ 
    if(selectedIndexPath != nil){ 
     let prevSelectedIndexPath = IndexPath(row: selectedIndexPath.row, section: selectedIndexPath.section) 
     selectedIndexPath = nil 
     self.collectionView.reloadItems(at: [prevSelectedIndexPath]) 
    } 
} 
+0

これは、選択された動物にはより暗い画像を与えるが、別の動物を選択すると、以前選択した動物はまだ暗くなる。これをどうやって解決するのですか? – fphelp

+0

また、明確なボタンがあり、それを押すとラベルは「選択された動物がありません」に変わります。このコードのどの部分をIBActionコードで使用する必要があるのですか?クリアボタンを押すと、すべての画像が正常に戻りますか? – fphelp

+0

@fphelp私の答えを確認してください私は最後のrequerimentのために必要な実装でclearSelection関数を追加しました –

1

を、あなたはUICollectionViewCellをサブクラス化しisSelectedプロパティをオーバーライドする必要があります。

class MyCell: UICollectionViewCell { 
    override var isSelected { 
     willSet(bool) { 
      self.backgroundColor = UIColor.gray // or whatever 
     } 
    } 
} 
関連する問題