13
12

More than 5 years have passed since last update.

CollectionViewの選択フレームワーク

Last updated at Posted at 2016-02-14

わかりずらいコレクションビューの選択フレームワークをまとめておく

選択フレームワーク

コレクションビュー自体にセルの選択状態を複数持たせれる。
チェックボックスのようなイメージ
スクリーンショット 2016-02-14 午前11.09.56.png
灰色 → 非選択
黒色 → 選択
赤色 → チェックボックスの代わり

使い方

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でタップが拾えるので頑張る

結論

自分で選択状態管理したほうが良さげ

13
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
12