LoginSignup
1
4

More than 5 years have passed since last update.

UITableViewをStoryBoardで作ってリスト表示

Last updated at Posted at 2019-01-14

つくるもの

 コード内に記述したArray型の文字列をUITableViewに反映させる

let dataList = ["テスト1","テスト2","テスト3","テスト4"]

UI

 Table Viewを置きます。
スクリーンショット 2019-01-13 20.18.03.png

 "Add New Constrains"で全てのSpacingを0にし、"Constrain to margins"をオフにするとTable Viewが全体に広がります。

 次にTable Viewの上にTable View Cellをドラッグ&ドロップすると、中にセルが作られます。
スクリーンショット 2019-01-13 20.18.49.png

 最終的にいくつものセルを表示させたい場合でも、1つだけ配置すればOK。

 次にセルに名前をつけ、あとでコード内から編集できるようにする。

 View Controller Sceneのツリーから"Table View Cell"を選択。Attributes Inspectorの"identifier"を"myCell"にする。
スクリーンショット 2019-01-13 20.19.27.png スクリーンショット 2019-01-13 20.19.44.png

 Enterを押して、ツリー内の"Table View Cell"が"myCell"になったか確認

スクリーンショット 2019-01-13 20.19.53.png

プロトコルの関連づけ

 UITableViewを扱うためにUITableViewDataSourceUITableViewDelegateというプロトコルを使います。

 ~DataSourceはテーブルの表示にどんなデータを使うか、~Delegateはテーブルが操作されたときにどんなことをするかを定義します。

Table ViewをView Controllerと関連づける

 View Controller SceneのツリーからTable Viewを選択し、
 スクリーンショット 2019-01-13 20.42.00.png

 Connections Inspectorボタンから関連づけます。OutletsのdataSource, delegateの項目の右端にある○をドラッグし、StoryBoardのView Controlerにドラッグしてください。
スクリーンショット 2019-01-13 20.44.38.png

 dataSourceとdelegateにView Controllerが関連づけられたことを確認してください。
スクリーンショット 2019-01-13 20.48.14.png

コード内でプロトコルを利用する

 ViewController.swiftを開き、UIViewControllerクラスにそれぞれのプロトコルを利用するよう宣言します。以下のように追記してください。

ViewController.swift
import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
}

データを用意する

 関連付けが終わったらまずは、Table Viewに表示するデータリストを用意します。

 viewDidLoad()の前に以下を追記します。これがリスト表示されるデータです。

ViewController.swift
    let dataList = ["テスト1","テスト2","テスト3","テスト4"]

Table Viewの処理する場所を作る

 先ほど2つのプロトコルを追記したらエラーが出たと思います。
スクリーンショット 2019-01-13 20.56.11.png

 Fixしてしまいましょう。以下のように追記してくれます。

ViewController.swift
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        <#code#>
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        <#code#>
    }

 少し見にくいので以下のように順序を入れ替えましょう。

ViewController.swift
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    let dataList = ["テスト1","テスト2","テスト3","テスト4"]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        <#code#>
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        <#code#>
    }
}

テーブルの行数の指定

 tableView: numberOfRowsInSection:以下のように記述します。

ViewController.swift
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataList.count
    }

 テーブルに表示したい列の数を返してあげます。例えば3行だけ表示したければreturn 3にします。ここでは先ほど定義したdataListのアイテム数を返すようにしています。

セル内の文字の変更

 tableView: cellForRowAt:には以下のように記述します。

ViewController.swift
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath)
        cell.textLabel?.text = dataList[indexPath.row]
        return cell
    }

 tableView.dequeueReusableCellで再利用可能なセルを取得しますそのパラメーターとしてwithIdentifier:は先ほど名付けた"myCell"が来てfor: にはindexPathを渡します

 cell.textLabel?.textはセル内のLabelを参照しそこにdataListの中の[indexPath.row]番目のデータを取り出します

完成コード

ViewController.swift
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    let dataList = ["テスト1","テスト2","テスト3","テスト4"]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataList.count
    }


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath)
        cell.textLabel?.text = dataList[indexPath.row]
        return cell
    }
}

完成イメージ

 ビルドしてこのように表示されれば成功です。

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