インデックスの位置が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を返すため
を助け
cellForItemAtIndexPath
で最後に
セルに
prepareForReuse
を実装:私は私の答えを更新したことが助け場合は外観とレムが知っていてください。 –