①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の部分)