2017-11-17 11 views
-1

インデックスの位置がnullでなくても、非表示のセルを取得するかどうかは、セルを取得する場合と取得しない場合があります。カスタムレイアウトコレクションビューで非表示セルの選択を解除するにはどうすればよいですか?

要約:shouldSelectItemAt関数内でロジックの選択と選択解除を試みました。選択はうまくいく。しかし、新しいセルを選択する際には、以前に選択されたセルを選択解除する必要があった。私はカスタムコレクションビューのレイアウトを使用しているので、私は問題がセルの再利用性のために発生しているとは思わない。

コード:

func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool { 
if(collectionView == customContentCollectionView){ 
let cell:MyContentCell = collectionView.cellForItem(at: indexPath)! as! MyCollectionViewController.MyContentCell 

// DESELECTION CODE 
if(previouslySelectedIndex != nil){ 
            // following line is error prone (executes but may or may not fetch the cell, sometimes deselect sometimes doesnt) 
            let prevCell = try collectionView.cellForItem(at: previouslySelectedIndex) as? MyCollectionViewController.MyContentCell 

            // Also tried with this following (executes but fails sometimes, in case not fetching the cell) 
            //let prevCell = try collectionView.cellForItem(at: previouslySelectedIndex)! as! MyCollectionViewController.MyContentCell 

            // Tried this one as well, fetching the previously selected cell using datasource, not directly from collection view 
            // let prevCell = customContentCollectionView.dataSource?.collectionView(collectionView, cellForItemAt: previouslySelectedIndex) as? MyCollectionViewController.MyContentCell 

            prevCell?.shapeLayer.strokeColor = bubbleBorder.cgColor 
            prevCell?.shapeLayer.fillColor = bubbleFill.cgColor 
            prevCell?.shapeLayer.shadowOpacity = 0.0 
            prevCell?.labelCount.textColor = bubbleBorder 
} 

// SELECTION CODE HERE GOES PRETTY WELL 
... 
previouslySelectedIndex = indexPath 
} 

N.B. :私はCustomUICollectionViewFlowLayoutを使用しています、私はshouldSelectItemAt関数を使用する必要があります。選択と選択解除のための他の機能はありません。ユーザースクロールcollectionViewセルは、したがって、いくつかの他のindexPathのために再利用されている可能性がある場合はnilを返すため

+0

を助けcellForItemAtIndexPath

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { //your code to create cell n all if(indexPath == previouslySelectedIndex) { //your code to select the cell } return cell } 

で最後に

override func prepareForReuse() { //reset all the fields of cell self.shapeLayer.strokeColor = bubbleBorder.cgColor self.shapeLayer.fillColor = bubbleFill.cgColor self.shapeLayer.shadowOpacity = 0.0 self.labelCount.textColor = bubbleBorder } 

セルにprepareForReuseを実装:私は私の答えを更新したことが助け場合は外観とレムが知っていてください。 –

答えて

0
let prevCell = try collectionView.cellForItem(at: previouslySelectedIndex) as? MyCollectionViewController.MyContentCell 

はnilを返します。

は、だからではなく、あなたがそれが再利用される前に、細胞の状態をリセットするためにprepareForReuseを用いることであろうデータソース

let cell = self.collectionView?.dataSource?.collectionView(self.collectionView!, cellForItemAt: IndexPath(row: 0, section: 0)) 

他のアプローチを尋ねるべきcollectionViewからセルを求めるべきではありません。 prepareForReuseは、しかし、私に多くの合法的なソリューションに聞こえる:)

編集:上記のコードはロード/悪影響につながる再利用されないセルのUIを更新し、セルを返しますけれども考え直しで

。セルが利用可能である場合にのみshouldSelectItemAt更新選択解除セルのUIにむしろ何を行うことができますです

func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool { 
     if(collectionView == customContentCollectionView){ 
      let cell:MyContentCell = collectionView.cellForItem(at: indexPath)! as! MyCollectionViewController.MyContentCell 

      // DESELECTION CODE 
      if(previouslySelectedIndex != nil){ 
       // following line is error prone (executes but may or may not fetch the cell, sometimes deselect sometimes doesnt) 
       if let prevCell = try collectionView.cellForItem(at: previouslySelectedIndex) as? MyCollectionViewController.MyContentCell { 

        // Also tried with this following (executes but fails sometimes, in case not fetching the cell) 
        //let prevCell = try collectionView.cellForItem(at: previouslySelectedIndex)! as! MyCollectionViewController.MyContentCell 

        // Tried this one as well, fetching the previously selected cell using datasource, not directly from collection view 
        // let prevCell = customContentCollectionView.dataSource?.collectionView(collectionView, cellForItemAt: previouslySelectedIndex) as? MyCollectionViewController.MyContentCell 

        prevCell?.shapeLayer.strokeColor = bubbleBorder.cgColor 
        prevCell?.shapeLayer.fillColor = bubbleFill.cgColor 
        prevCell?.shapeLayer.shadowOpacity = 0.0 
        prevCell?.labelCount.textColor = bubbleBorder 
       } 
      } 

      // SELECTION CODE HERE GOES PRETTY WELL 
       previouslySelectedIndex = indexPath 
     } 
    } 

上記追加letが、それがある場合にのみ、あなたがそれを解除するには、セルのUIを更新確保の世話をする必要がある場合は利用可能です。今

希望これは

関連する問題