2017-11-29 13 views
0

イム私はコレクションビューをスクロールするとき、私はこれは私が選択したときに色を変更するには、自分のコードで新しいものIOS UICollectionViewセル選択と選択解除問題

を選択したときに、私のタブバー としてUIcollectionビューを使用して水平に前回選択されたセルが選択解除しませんスクロール細胞はそう、あなたのコード内でいくつかの変更を変更する必要が呼び出されますその時cellForItemAtを再利用している間、セルとセル

var selectedIndexPath : IndexPath = [] 
func collectionView(_ collectionView: UICollectionView, 
didSelectItemAt indexPath: IndexPath) { 

    if let cell = collectionView.cellForItem(at: indexPath) as? 
    BottomCollectionViewCell { 
     cell.contentView.backgroundColor = UIColor.orange 
     cell.backgroundColor = UIColor.orange 
    } 

    if let preViousSelectedcell = collectionView.cellForItem(at: 
    selectedIndexPath) as? BottomCollectionViewCell { 

    preViousSelectedcell.contentView.backgroundColor=UIColor.purple 
    preViousSelectedcell.backgroundColor = UIColor.purple 
} 
selectedIndexPath = indexPath 


} 
+0

使用 'collectionViewのに役立ちますあなたのcollectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath)

if indexPath == selectedIndexPath { cell.contentView.backgroundColor=UIColor.purple cell.backgroundColor = UIColor.purple } else { cell.contentView.backgroundColor = UIColor.orange cell.backgroundColor = UIColor.orange } 

希望内側の線の下に追加します(_ collectionView:UICollectionView、didDeselectItemAt indexPath:IndexPath)' –

+0

を私はすでに何もchanged.I使用私はビューをスクロールすると、インデックスパスが変化していると思う。 –

+0

あなたはあなたの答えを更新して、あなたがそれをどのように使用したかを示してください。 –

答えて

3

の選択を解除

func collectionView(_ collectionView: UICollectionView, 
didSelectItemAt indexPath: IndexPath) { 
selectedIndexPath = indexPath 
YOUR_COLLECTION_VIEW.reloadData() 
} 

これはあなた

関連する問題