0
2

More than 3 years have passed since last update.

#8 XcodeのTableViewの使い方1例

Last updated at Posted at 2019-11-20

はじめに

個人のメモ程度の出来なのであまり参考にしないで下さい.

環境

Xcode:11.2.1
Swift:5.1.2
2019/11

part1

XcodeでTableViewと検索する.

スクリーンショット 2019-11-20 午前0.09.04.png

part2

UITableViewを置きたいViewに乗せる.
制約を付ける.
先にbannerViewを置くこと.

スクリーンショット 2019-11-20 午前0.09.28.png

part3

XcodeでTableViewCellと検索する.

スクリーンショット 2019-11-20 午前0.09.55.png

part4

UITableViewCellを先ほど置いたUITableViewに乗せる.

スクリーンショット 2019-11-20 午前0.10.07.png

part5

Cellを選択した状態で,Attributes inspectorIdentifierにCellを識別するための任意の文字列を入力する.

スクリーンショット 2019-11-20 午前0.15.26.png

part6

ここからはFirstViewControllerのみ.
Cellを選択したときに表示するUIViewControllerを置く.

スクリーンショット 2019-11-20 午前0.16.06.png

part7

Cellを選択した状態で,contrlキーを押しながら先ほど置いたUIViewControllerにドラッグ・ドロップする.
Showを選択する.

スクリーンショット 2019-11-20 午前0.16.18.png

part8

画面を分割し,Main.storyboardTable Viewを選択する.

スクリーンショット 2019-11-20 午後9.29.15.png

part9

contrlキーを押しながらドラッグ・ドロップで,コードと接続する.

スクリーンショット 2019-11-20 午後9.28.16.png

part10

  • FirstViewControllerクラスにUITableViewDataSource, UITableViewDelegateの2つを批准させる.
  • viewDidLoad()メソッドにnamesTableView.delegate = selfnamesTableView.dataSource = selfの2つを追加する.
  • tableView(_:numberOfRowsInSection:)メソッドを追加する.
  • tableView(_:cellForRowAt:)メソッドを追加する.

下が追加後のコード

import UIKit

//↓↓↓↓↓デリゲートを追加
class FirstViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var namesTableView: UITableView!


    override func viewDidLoad() {
        super.viewDidLoad()

        //↓↓↓↓↓追加
        namesTableView.delegate = self
        namesTableView.dataSource = self
    }

    //↓↓↓↓↓追加
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }

    //↓↓↓↓↓追加
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "nameCell", for: indexPath)
        cell.textLabel?.text = "Test"
        return cell
    }
}

part11

ビルドしてTableViewが使えたらOK.

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