2017-08-16 57 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

可以定義var稱爲selectedIndexpath,並在您didSelectItemAt通過選定的索引路徑更改方法,如果selectedIndexPath相同,則應使selectedIndexpath爲零,並且在您的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檢查我的答案我已經添加了一個clearSelection函數,您需要的實現爲最後的requeriment –

1

爲了做到這一點,你必須繼承UICollectionViewCell並覆蓋isSelected屬性:

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