おとうさまくんの手作り工房

日曜大工としてのプログラミングを紹介します。

【iOS備忘録】UITableView 表示から選択まで

最初にデリゲートなど設定

ストーリーボードでUITableViewを貼り付け、

@property (weak, nonatomic) IBOutlet UITableView *tableView;

 

UITableViewのプロトコルを実装し、

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>

...

viewDidLoadにデリゲートメソッドを実装する。

    self.tableView.delegate = self;

    self.tableView.dataSource = self;

 

結果、interface部とviewDidLoadは以下のようになります。

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) NSArray *array1,*array2;

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];    

    // デリゲートメソッド実装

    self.tableView.delegate = self;

    self.tableView.dataSource = self;

    

    self.array1 = @[@"aaa",@"bbb",@"ccc"];

    self.array2 = @[@"111",@"222",@"333"];

}

 

テーブルに関する設定

ここでは2つのセクションに、2つの配列array1,array2を表示させることにします。

//まず、セクション数を設定し、

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 2;

}

 

//セクションごとにデータが何件あるか設定し、

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    NSInteger dataCount;

    switch (section) {

        case 0:

            dataCount = self.array1.count;

            break;

        case 1:

            dataCount = self.array2.count;

            break;

        default:

            break;

    }

    return dataCount;

}

 

//セクションごとのテーブルセルにデータを設定します。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (!cell) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault

                                      reuseIdentifier:CellIdentifier];

    }

    switch (indexPath.section) {

        case 0:

            cell.textLabel.text = self.array1[indexPath.row];

            break;

        case 1:

            cell.textLabel.text = self.array2[indexPath.row];

            break;

        default:

            break;

    }    

    return cell;

}

 

選択したセルを取得する

//選択されたセルのセクションと列番号を取得します。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSLog(@"選択したセクションは%i,列番号は%i",(int)indexPath.section,(int)indexPath.row);

    switch (indexPath.section) {

        case 0:

            NSLog(@"選択したText%@",self.array1[indexPath.row]);

            break;

        case 1:

            NSLog(@"選択したText%@",self.array2[indexPath.row]);

            break;

        default:

            break;

    }   

}

 

見出しもつけてみる

//セクションごとのタイトルを設定(任意)

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    switch(section) {

        case 0:

            return @"セクション英文字";

            break;

        case 1:

            return @"セクション数字";

            break;

    }

    return nil;

}

表示と結果

左が表示直後、右がセルを選択した状態です。

f:id:otousamakun:20160105124100p:plainf:id:otousamakun:20160105124125p:plain

出力ログは以下のとおりです。

 

 選択したセクションは1,列番号は0

 選択したText111