LoginSignup
0
1

More than 3 years have passed since last update.

TableViewセルの作り方 その2(Xib)

Last updated at Posted at 2020-12-05

環境

Xcode 12.2

1.Xibファイルの作成

  • Project navigatorでNew File...

スクリーンショット 2020-12-05 15.40.15.png

  • User InterfaceのViewを選択してNext → Xib名を入力してCreate

スクリーンショット 2020-12-05 15.47.41.png

2.TableViewCellの作成

  • デフォルトのViewを消す(選択してdelete)

スクリーンショット 2020-12-05 15.50.20.png

  • (+)をクリックしてLibraryからTableViewCellを選択しXibファイルにドラッグアンドドロップ

スクリーンショット 2020-12-05 15.56.05.png

  • できたセル
    スクリーンショット 2020-12-05 16.04.06.png

  • UIパーツを配置して、レイアウトの制約を追加する

3. UITableViewCell クラスの作成

仮にSecondTableViewCellというUITableViewCell クラスにした場合

SecondTableViewCell.swift
class SecondTableViewCell :UITableViewCell{

  override func awakeFromNib() {
    super.awakeFromNib()
  }

  override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

  }

4. XibとUITableViewClassの紐付け

  • 使用するUITableViewCellクラスの設定

スクリーンショット 2020-12-05 23.15.00.png

5. CellIdの設定

スクリーンショット 2020-12-05 22.53.05.png

後でTableViewに登録する際に使う

6. UIをUITableViewCellクラスに紐付ける(ついでにUIのプロパティーも変更)

SecondTableViewCell.swift
class SecondTableViewCell :UITableViewCell{

  //★★★UIとの接続
  @IBOutlet weak var cellImageView: UIImageView!
  @IBOutlet weak var textView: UITextView!

  override func awakeFromNib() {
    super.awakeFromNib()

    //CellのUIのプロパティー変更
    cellImageView.layer.cornerRadius = 25
  }

  override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
  }
}

7. TableViewへのセルの登録

viewDidLoad内などに記述


private let cellId = "cellId"

  @IBOutlet weak var secondTableView: UITableView!

  override func viewDidLoad() {
    super.viewDidLoad()

    //セルの登録(Xibファイル名:SecondViewTableViewCell.xibの場合)
    secondTableView.register(UINib(nibName: "SecondViewTableViewCell", bundle: nil), forCellReuseIdentifier: cellId)

  }
}

出来上がり

スクリーンショット 2020-12-06 0.13.13.png

全コード

SecondTableViewCell.swift
import UIKit

class SecondTableViewCell :UITableViewCell{

  //cellIdを決めて格納
  private let cellId = "cellId"

  //★★★UIとの接続
  @IBOutlet weak var cellImageView: UIImageView!
  @IBOutlet weak var textView: UITextView!

  override func awakeFromNib() {
    super.awakeFromNib()

    //CellのUIのプロパティー変更
    cellImageView.layer.cornerRadius = 25

  }

  override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

  }
}
SecondViewController.swift
import UIKit

class SecondViewController : UIViewController{


  private let cellId = "cellId"

  @IBOutlet weak var secondTableView: UITableView!
  override func viewDidLoad() {
    super.viewDidLoad()

    secondTableView.delegate = self
    secondTableView.dataSource = self

    //セルの登録(Xibファイル名:SecondViewTableViewCell.xibの場合)
    secondTableView.register(UINib(nibName: "SecondViewTableViewCell", bundle: nil), forCellReuseIdentifier: cellId)
  }
}

extension SecondViewController :UITableViewDelegate,UITableViewDataSource{

  //セルの高さを指定
  func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 100
  }
  //セルの個数
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 10
  }
  //セルの内容
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = secondTableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
    cell.backgroundColor = .lightText

    return cell
  }
}

最後に

私の理解が間違っていたらご指摘いただければ幸いです。

0
1
0

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
0
1