わかりずらいコレクションビューの選択フレームワークをまとめておく
選択フレームワーク
コレクションビュー自体にセルの選択状態を複数持たせれる。
チェックボックスのようなイメージ
灰色 → 非選択
黒色 → 選択
赤色 → チェックボックスの代わり
使い方
self.collectionView?.allowsMultipleSelection = true
でOK
何が難しい
1.よくあるインターフェースで、セルをタップしたら詳細画面、右上のチェックボックを押したら
選択にしたい場合。チェックボタンを押しても、セルは選択状態にならない。セルをタップすると
選択状態になる><
2.reloadData系を呼ぶと、選択状態が解除される。
ソリューション
1.ボタンをタップした時にselected
を見て選択/非選択にする
if cell.selected {
collectionView.deselectItemAtIndexPath(indexPath, animated: true)
}else{
collectionView.selectItemAtIndexPath(indexPath, animated: true, scrollPosition: UICollectionViewScrollPosition.Bottom)
}
2.セルのタップで選択/非選択無効化
func collectionView(collectionView: UICollectionView, shouldSelectItemAtIndexPath indexPath: NSIndexPath) -> Bool{
return false
}
func collectionView(collectionView: UICollectionView, shouldDeselectItemAtIndexPath indexPath: NSIndexPath) -> Bool{
return false
}
3.詳細画面への遷移をどうする?
↑の2つのAPIでタップが拾えるので頑張る
結論
自分で選択状態管理したほうが良さげ