LoginSignup
1
0

More than 1 year has passed since last update.

[Swift] [Xcode] xibを使用してUITableViewを実装してみた

Last updated at Posted at 2022-02-15

#完成品
スクリーンショット 2022-02-15 19.46.48.png

#実装
##tableView
controllerにtableViewを追加します
スクリーンショット 2022-02-15 19.26.04.png

スクリーンショット 2022-02-15 19.26.30.png

tableViewを選択した状態でcontrollerにoutlet接続します
スクリーンショット 2022-02-15 19.27.04.png

スクリーンショット 2022-02-15 19.27.19.png

##xibファイルの作成
続いてxibファイルの作成です。
左上のFile→New→Fileを選択します。
スクリーンショット 2022-02-15 19.29.05.png

ここではCocoa Touch Classを選択してください。
スクリーンショット 2022-02-15 19.29.34.png

SubClass ofをUITableViewCellにしてください。またAlso create Xib fileにチェックが入れてください。これらが確認できたらClass名をつけます。ここではGafaTableViewCellとしています。ここまでできたらNextを押してください。
スクリーンショット 2022-02-15 19.30.04.png

GafaTableViewCellのxibファイルができました。右上の+ボタンを選択しcellにLabelを追加してください。
スクリーンショット 2022-02-15 19.30.43.png

labelに対して、先ほど生成したGafaTableViewCellファイルにoutlet接続します。
スクリーンショット 2022-02-15 19.32.01.png

ここではgafaLabelとしておきます。
スクリーンショット 2022-02-15 19.32.19.png

Custum Classの下のClassがGafaTableViewCellになっていることを確認してください。
スクリーンショット 2022-02-15 20.56.40.png

Identifierが空になっていることを確認してください。
スクリーンショット 2022-02-15 20.57.09.png

Identifierをcellと入力します。
スクリーンショット 2022-02-15 20.56.58.png

##ViewController
ViewControllerの全体のコードを先に載せます。
このままコピペしてビルドすれば完成です。

ViewController.swift
import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!
    let gafaList = ["Google","Amazon","FaceBook","Apple"]
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(UINib(nibName: "GafaTableViewCell", bundle: nil), forCellReuseIdentifier: "cell")
    }


}
extension ViewController:UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return gafaList.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! GafaTableViewCell
        cell.gafaLabel.text = gafaList[indexPath.row]
        return cell
    }
}

カスタムセルでない一般的なTableView?と違う点は下記のコードだと思います。nibNameは先ほど作成したXibファイルのファイル名を、bundleは基本nilでforCellReuseIdentifierは先ほど設定したidentifier(=cell)を書いてください。

tableView.register(UINib(nibName: "GafaTableViewCell", bundle: nil), forCellReuseIdentifier: "cell")
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? GafaTableViewCell else { fatalError("The dequeued cell is not instance") }
        cell.gafaLabel.text = gafaList[indexPath.row]
        return cell
    }

#完成品
完成品のコード
スクリーンショット 2022-02-15 19.46.48.png

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