LoginSignup
0
1

More than 3 years have passed since last update.

10分でTableviewを作ってみた。

Last updated at Posted at 2020-08-09

やること

Tableviewを作る

セルに表示させるアイテムを定義する

 let items = ["リンゴ","バナナ","オレンジ"]

Tableviewを作る

①Tableviewを作るコードを記入


    let tableView: UITableView = {
        let tv = UITableView()
        return tv
    }()

override func viewDidLoadの中にtableviewを表示させるコードを記入

view.addSubview(tableView)

③tableviewのサイズを指定

tableView.frame.size = view.frame.size

シミュレーター

スクリーンショット 2020-07-28 20.52.14.png

tableviewのデリゲートのメソットの設定

override func viewDidLoadの中にデリゲートのメソットを使えるようにするコードを記入

tableView.delegate = self
tableView.dataSource = self

これでデリゲートのメソットが使えるようになりました。

②classの一番上のところにprivate let cellId = "cellId"と記入

③一番下にデリゲート使うにあたって必ず必要なメソッドを記入

extension ViewController: UITableViewDelegate,UITableViewDataSource{
 //セルを何個表示すさせるかのコード
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.items.count   
            }

//セルに何を表示させるかのコード
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let  cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
cell.textLabel?.text = self.items[indexPath.row]  
    return cell
}
}

override func viewDidLoadの中にTableViewに上記のメソッドを反映させるコードを記入

tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellId)

タイトルをつける

①Main.storyboardに移動してViewControllerを選択し、EditorEmbedldNavigationControllerを選択してNavigationControllerを設置する。
スクリーンショット 2020-07-28 21.42.32.png

②ViewController.swiftに戻ってoverride func viewDidLoadの中にタイトルを表示させるコードを入力する

navigationItem.title = "果物"

シミュレーター

スクリーンショット 2020-08-09 22.10.02.png

全体のコード

class ViewController: UIViewController {

      let items = ["リンゴ","バナナ","オレンジ"]

       private let cellId = "cellId"

//tableviewを作っていく
    let tableView: UITableView = {
        let tv = UITableView()
        return tv
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(tableView)
        tableView.frame.size = view.frame.size

        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
        navigationItem.title = "果物"


    }
}

extension ViewController: UITableViewDelegate,UITableViewDataSource{


        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

            return self.items.count
            }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)

         cell.textLabel?.text = self.items[indexPath.row]

         return cell


    }


}

今回はほとんどコードでtableviewを表示させてみました!

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