1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Swift】TableViewCellの使い方(自分用メモ)

Last updated at Posted at 2021-09-22

はじめに

UITableViewにTableViewを用いてカスタムビューを表示するまでの流れでステップがいくつかあったので、自分用にメモします。

下準備

  1. 右クリック → NewFileを選択 → Cocoa Touch Classを選択


2. Class名を任意の値に変更
3. SubClassをUITableViewCellに変更
4. Also create XIB fileにチェックを入れて次へ

カスタムViewの作成

  1. AutoLayoutを適応しながら1つのCellのレイアウトを決定
  2. それぞれの要素を関連付け
  3. Cell生成時に値を代入するための関数(ここではsetCellに該当)を用意
  4. XibファイルのViewのIDを任意のものに変更

TableViewへの反映

スクリーンショット 2021-09-23 1.22.43.png

  1. delegateとdatasourceを継承
  2. delegateとdatasourceを設定(ViewDidload)
  3. TableViewCellのIDを変更(Attributes Inspector)
  4. TableViewCellの大きさをAutomaticに変更(Size Inspector)
  5. TableViewにカスタムセルを登録(registerメソッド)
  6. TableViewのCellにIDを付与
  7. cellForRowAtでcellを定義する際、6のIDを指定して取得する
  8. cellForRowAtで定義したcellを作成したカスタムビューの型にキャスト
  9. cellForRowAtでsetCell関数を実行してCell内部のオブジェクトに値を代入
  10. テーブル名.rowHeight = UITableView.automaticDimensionをViewDidloadに追加

みたいな感じで実装したら、ええ感じに実装できる。

1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?