14
15

More than 5 years have passed since last update.

TableViewやCollectionViewを使ったときに考えそうな機能2つ[初心者向け]

Last updated at Posted at 2014-05-18

リスト系のビューを使うときに、おもにナビゲーションバーあたりのボタンに実装したくなりそうな機能を2つ用意したのでメモ。なお、今回はCollectionViewを利用している。

選択中のアイテムを一括で解除する

コレクションビュー内のアイテム(セル)の数だけ、deselectItemAtIndexPath:を呼び出す。

- (void)clearCellSelections {
    for (NSIndexPath *indexPath in self.collectionView.indexPathsForSelectedItems) {
        [self.collectionView deselectItemAtIndexPath:indexPath animated:NO];
    }
}

参考サイト

普段はタブバーを表示するが、特定の条件下ではツールバーを表示する

特定の条件が成立したら
1.タブバーの座標をスクリーン外に設定する

    //TabBarのサイズを取得
    float t_height = self.tabBarController.tabBar.frame.size.height; //t_height = 49とする

    //TabBarを画面外に移動
    self.tabBarController.tabBar.frame = CGRectMake(0.0f, 568.0f, 320.0f, t_hight);

2.ツールバーをタブバーがあった場所に表示する

    self.toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 519.0f, 320.0f, 49.0f)];     


    [self.view.superview addSubview:self.toolBar]; //ツールバーは上位ビューに配置しないとタップを認識してくれない(*)
    NSLog(@"self.view.superview is %@", self.view.superview);
    NSLog(@"self.view is %@", self.view);

(*)について確認すると、コレクションビューにツールバーを追加する場合、親ビュー外に子ビューをレイアウトしてしまうことがわかる。

2014-05-18 22:30:25.056 XXX[1397:60b] self.view.superview is <UIViewControllerWrapperView: 0x14d72660; frame = (0 0; 320 568); autoresize = W+H; layer = <CALayer: 0x14d726d0>>
2014-05-18 22:30:25.057 XXX[1397:60b] self.view is <UICollectionViewControllerWrapperView: 0x14d5c360; frame = (0 0; 320 519); autoresize = W+H; layer = <CALayer: 0x14d60970>> //縦の長さが519まで!

3.状況が変わったら元に戻す

    //TabBarのサイズを取得
    float t_hight = self.tabBarController.tabBar.frame.size.height;

    //TabBarを画面内に移動
    self.tabBarController.tabBar.frame = CGRectMake(0.0f, 519.0f, 320.0f, t_hight);

参考サイト

14
15
2

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
14
15