LoginSignup
20
19

More than 5 years have passed since last update.

UITableViewのCellを選択してチェックをつけて、

Last updated at Posted at 2013-06-04

①UITableViewのCellを選択してチェックをつけて、
②それ以外のセルはチェックを外して、
③前の画面に戻る

というピンポイントな内容の実装について。
セルを選択した時に実行されるメソッドtableView: didSelectRowAtIndexPath:に書いていきます。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // テーブルを更新
    [self.tableView reloadData];

    // ②選択したセル以外のすべてのチェックを取る
    // 今回はセクションは「0」(一番初めのセクション)とします。
    for (NSInteger index=0; index<[self.tableView numberOfRowsInSection:0]; index++) {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
        cell.accessoryType = UITableViewCellAccessoryNone;
        // ①選択したセルだけチェックする
        if (indexPath.row == index) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
    }

    // ③前の画面に戻る
    [self.navigationController popViewControllerAnimated:YES];
}

各セクションのセルの数は[self.tableView numberOfRowsInSection:【セクション】]
って感じで取れます。(上記forの部分)

20
19
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
20
19