LoginSignup
6
10

More than 5 years have passed since last update.

UICollectionViewで、プログラム側からCellを選択状態にしようとしたら、ハマったお話

Last updated at Posted at 2015-08-21

そもそもCellを選択状態にするにはどうするの?

(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
の中で、UICollectionViewCellを取得して、selectedプロパティをYESにすればいいんですけど。

何にハマったの?

選択状態になったことはなったんですが、UIでタップしても非選択状態にならなくなりました。

どうやって解決するの?

UICollectionViewCellのselectedをYESにした後で、 [collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone]; って呼んであげましょう。

セルのselectedプロパティをYESにする方法は正しくありません。
最終的にはviewWillAppearでselectItemAtIndexPathを呼び出しました。
ただ、これは私のプログラムで選択状態にしたいデータを取ってくるタイミングがここだったからです。おそらく、viewDidLoadでも可能じゃないかと思います。(未確認)

6
10
8

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
6
10