2017-07-28 11 views
0

私のアプリでは、UICollectionViewでセルを選択した場合、境界線が青色になり、別のセルを選択すると、境界線が透明になるはずです。didDelectlectItemAt別のセルが選択されたときにindexPathがトリガーされない

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! ChatCell 

     /* Set some settings */ 
     if globalSelected[indexPath.item] { 
      cell.circleView.layer.borderColor = UIColor.blue.cgColor 
     } else { 
      cell.circleView.layer.borderColor = UIColor.clear.cgColor 
     } 

     return cell 
} 

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    //Global variable for maintain selection 
    global.selectedChatPath = indexPath 
    globalSelected[indexPath.item] = true 
    collectionView.reloadData() 
} 

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) { 
    if indexPath != nilPath { 
     globalSelected[indexPath.item] = false 
     collectionView.reloadData() 
    } 
} 

nilPathがちょうどIndexPath(:-1、セクション:アイテム0)である:私が書いた方法があるが、それは問題ではありませんが、理由collectionView(_ collectionView:UICollectionView、didDeselectItemAt indexPath:IndexPath)は呼び出されません。私のCollectionViewはですallowSelection = trueallowsMultipleSelection = falseプロパティです。私はどんな助けでも感謝しています。

+0

CollectionViewの代理人を追加する "collectionView.delegate = self" – Ragul

+0

@Ragulありがとうございます! UICollectionViewDataSourceとUICollectionViewDelegateのプロトコルに準拠した別のクラスがあり、このクラスのインスタンスをUICollectionViewのデリゲートとデータソースとして宣言しました。また、最初の手順はうまくいきます:セルをクリックした後、枠線が青色に変わります(委任がうまくいくことを意味します)。しかし、他をクリックすると枠線も青色になりますが、前の枠線細胞はまだ青い(私は透明にしたい)。 –

+0

ちょうど選択ごとにcollectionViewを再ロード – Ragul

答えて

3

単一のセルのみをすることになっている場合:あなたは別のセルをタップすると、以前に選択したセルの選択を解除するには、次のようにdidSelectItemfalseにグローバル変数で、以前に選択したセルの設定値を設定する必要がありますcellForItemAt

var selectedIndexPath : IndexPath? 

変数インスタンスに応じて色を設定( nilが選択されている何を意味する)私はインスタンス変数に現在選択されているインデックスパスを置くことをお勧めします同時に選択すること

didSelectItemAt最後に選択したセルのみをリロードし、新しい選択したインデックスパスにselectedIndexPathを設定します。これは、コレクションビュー全体をリロードするよりも効率的です。

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    //Global variable for maintain selection 

    var cellsToReload = [indexPath] 
    if let selected = selectedIndexPath { 
     cellsToReload.append(selected) 
    } 
    selectedIndexPath = indexPath 
    collectionView.reloadItems(at: cellsToReload) 
} 

didDeselectItemAtは、セルを明示的に選択解除する場合にのみ必要です。

0
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! CollectionViewCell 
    if selectedIndex == indexPath.item { 
    cell.backgroundColor = .blue 
    } else { 
    cell.backgroundColor = .clear 
    } 
} 

とdidSelectItemAtで、

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    selectedIndex = indexPath.item 
    collectionView.reloadData() 
} 
+0

ありがとうございます!私は自分の質問を修正しました(今はcellForItemAtメソッドも含まれています)。 –

+0

あなたのコードとcollectionviewは同じコントローラにありますか? – Ragul

1

この

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! ChatCell 

     /* Set some settings */ 
     if globalSelected[indexPath.item] { 
      cell.circleView.layer.borderColor = UIColor.blue.cgColor 
      collectionView.selectItemAtIndexPath(indexPath, animated: false, scrollPosition: .None) 
     } 
     else { 
      cell.circleView.layer.borderColor = UIColor.clear.cgColor 
      collectionView.deselectItemAtIndexPath(indexPath, animated: false) 
     } 
     return cell 
     } 
0

は、セルを選択し、目的のセルの枠線を変更されるたび、あなたUICollectionViewをリロードしてみてください。 データをリロードすると、前のセルのボーダーが削除されます。その後、必要なセルにボーダーを追加することができます。

0

didDeselectItemは、選択したセルを再度タップするまで呼び出されません。

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    globalSelected[global.selectedChatPath.item] = false //Set the previously selected cell's setting to false 

    //Global variable for maintain selection 
    global.selectedChatPath = indexPath 
    globalSelected[indexPath.item] = true 
    collectionView.reloadData() 
} 
関連する問題