LoginSignup
4
6

More than 5 years have passed since last update.

コードでUICollectionViewのCellの選択状態の設定と解除

Last updated at Posted at 2016-01-06

選択状態の設定

.m
    // 複数選択する場合はallowsMultipleSelectionをYESに設定する
    _collectionView.allowsMultipleSelection = YES;

    // 一番最初のCellを選択する
    NSIndexPath *selectIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [_collectionView selectItemAtIndexPath:selectIndexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone];

選択状態の解除

複数選択の場合

.m
    // 選択されているIndexPathを全て非選択状態にする
    for (NSIndexPath *indexPath in _collectionView.indexPathsForSelectedItems) {
        [_collectionView deselectItemAtIndexPath:indexPath animated:NO];
    } 

単一選択の場合

.m
    [_collectionView deselectItemAtIndexPath:[_collectionView.indexPathsForSelectedItems lastObject] animated:NO];
4
6
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
4
6