スキップしてメイン コンテンツに移動

UITableViewCell


UITableViewCell

UITableViewCellクラスは、テーブル(UITableView)表示する時、1つ1つのセルを管理するクラスです。

UITableViewCellのクラス階層

NSObject

UIResponder

UIView

UITableViewCell

生成


// スタイルを指定した生成例
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell =
    [[UITableViewCell alloc] initWithStyle:スタイル名 reuseIdentifier:CellIdentifier];

// 空のセル生成例
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell =
    [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];

※サイズに関しては、サイズ指定のCGRect欄をご覧ください。
【スタイルの種類】
スタイル名説明
UITableViewCellStyleDefault画像(左)と主テキスト(右)があるセル
UITableViewCellStyleValue1主テキスト(左)とサブテキスト(右)があるセル
UITableViewCellStyleValue2サブテキスト(左)と主テキスト(右)があるセル
UITableViewCellStyleSubtitle画像(左)と主テキスト(右上)とサブテキスト(右下)があるセル

UITableViewCellのプロパティ

【UITableViewCellの主要プロパティ】
プロパティ名/型読専説明
textLabel.text
NSString
テキストを設定する
(例)cell.textLabel.text = @"ほげ";
textLabel.textColor
UIColor
テキストの色を設定する
(例)cell.textLabel.textColor = [UIColor blueColor];
textLabel.font
UIFont
フォントを設定する
(例)cell.textLabel.font =
  [UIFont systemFontOfSize:[UIFont systemFontSize]];
textLabel.textAlignment
(UITextAlignment)
横揃えを設定する
 中央寄せ: UITextAlignmentCenter
 左寄せ: UITextAlignmentLeft
 右寄せ: UITextAlignmentRight
(例)cell.textLabel.textAlignment = UITextAlignmentCenter;
detailTextLabel.text
NSString
テキストを設定する
(例)cell.detailTextLabel.text = @"ほげ";
detailTextLabel.textColor
UIColor
テキストの色を設定する
(例)cell.detailTextLabel.textColor = [UIColor blueColor];
detailTextLabel.font
UIFont
フォントを設定する
(例)cell.detailTextLabel.font =
  [UIFont systemFontOfSize:[UIFont systemFontSize]];
detailTextLabel.textAlignment
(UITextAlignment)
横揃えを設定する
 中央寄せ: UITextAlignmentCenter
 左寄せ: UITextAlignmentLeft
 右寄せ: UITextAlignmentRight
(例)cell.detailTextLabel.textAlignment = UITextAlignmentCenter;
imageView.image
UIImage
画像を設定する
(例)cell.imageView.image =
     [UIImage imageNamed:@"hoge.png"];
accessoryType
(UITableViewCellAccessoryType)
アクセサリの設定
UITableViewCellAccessoryDisclosureIndicator:
  DisclosureIndicator"
UITableViewCellAccessoryDetailDisclosureButton:
  DetailDisclosureButton
UITableViewCellAccessoryCheckmark:
  Checkmark
UITableViewCellAccessoryNone:
  なし
(例)cell.accessoryType = UITableViewCellAccessoryNone;
selectionStyle
(UITableViewCellSelectionStyle)
セル選択時の色の設定
UITableViewCellSelectionStyleBlue:青
UITableViewCellSelectionStyleGray:灰
UITableViewCellSelectionStyleNone:なし
(例)cell.selectionStyle = UITableViewCellSelectionStyleGray;
 主要なプロパティのみ掲載しています。
 上記「UITableViewCellのクラス階層」にあるクラスのプロパティも使用できます。

UITableViewCell例文


// テーブルビューセル例文
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell =
    [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if(cell == nil){
  cell = [[UITableViewCell alloc]
      initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  cell.textLabel.textColor = [UIColor blueColor];
  cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
  cell.selectionStyle = UITableViewCellSelectionStyleGray;
}

cell.textLabel.text = @"ほげ";


//セルの背景を青色にする
cell.contentView.backgroundColor = [UIColor blueColor];

コメント

このブログの人気の投稿

日本に来た 今三週間

四月六日に日本に来た、今三週間になった。 嬉しくなかった、悲しくなかった。 日本の携帯電話のカメラはとてもいいよ、私は今撮影が好きだ、いつも寮に帰る途中で花とか魚とか写真を撮る。これも生活の趣味だ。 滴は綺麗だ 桜が凋んで落ちる 天気がいいとき、自転車に乗って、食材を買った。 自分で料理をつくる、おいしそうでしょう。 昨日清水寺に行った、天気はとても熱いね。日に焼けすぎたので、腕は今赤い。 側に花があるので、赤い清水寺はもっと美しそうだ。 木の色は艶やかだ。