junpi-iosのブログ -2ページ目

junpi-iosのブログ

ブログの説明を入力します。

アプリ作成において頻繁に使うUITableViewの備忘録。

◻︎デリゲート&データソース◻︎
・UITableViewDataSource
・ UITableViewDelegate

◻︎生成◻︎
UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, 320, 454) style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];

◻︎データソース&デリゲートメソッド

// セクションの個数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return セクション;
}

// 行の個数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 行;
}

// セルの中身
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// "cell"というkeyでcellデータを取得
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
// cellデータが無い場合、UITableViewCellを生成して、"cell"というkeyでキャッシュする
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}

}

// セルがタップされた時のイベント
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// cellがタップされた際の処理
}

// 各行の高さを決めるメソッド
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 行の高さの総計;
}