[iOS] UITableViewで選択したセルにチェックマークをつけたい

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
きゃりーわんわん
記事: 34
登録日時: 11年前

[iOS] UITableViewで選択したセルにチェックマークをつけたい

#1

投稿記事 by きゃりーわんわん » 9年前

掲題の通り、選択したセルにチェックマークを付けるため、
以下のページを参考にしたのですが、チェックマークが付きません。
http://mako-wis.hatenablog.com/entry/2014/03/07/110322

セルが選択された際に実行されるメソッド内で
cell.accessoryType = UITableViewCellAccessoryCheckmark;
を実行しているので、問題なくチェックマークが付くと考えていますが、付きません。

間違っているところがあるためチェックマークが付かないと思うのですが、
そこがわからずでして、、、
お手数ですがご教示をお願いします。

◆ViewController.h

コード:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
◆ViewController.m

コード:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.tableView.delegate = self;
    self.tableView.dataSource = self;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [NSString stringWithFormat:@"section%ld", (long)section];
}

// セルの数:10
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

// indexPathで指定されたcellに値をセット
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }

    // セルの内容をindexPath.rowとする
    cell.textLabel.text = [NSString stringWithFormat:@"%d", indexPath.row];
    return cell;
}

// セルが選択された時に呼び出される
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // 選択されたセルを取得
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    // 選択されたセルの内容をログに出力
    NSLog(@"cell.textLabel.text=%@", cell.textLabel.text);
    
    // セルのアクセサリにチェックマークを指定
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

@end


“C言語何でも質問掲示板” へ戻る