1
2

More than 3 years have passed since last update.

【swift5】TableViewを使ってtodoリストを作成する

Posted at

今回やること

swift5を使用してtodoリストを作成します。

ソースとストーリーボード

こちらはストーリーボード

tableViewを置いてその上にtable cellを置いただけです。

スクリーンショット上ではわからない点としてはOutletsのdataSourceとdelegateをviewコントローラーを接続しています。

スクリーンショット 2019-12-25 20.00.04.png

こちらがviewControllerのソースです。

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    let todo = ["牛乳を買う", "髪を切る", "掃除をする"]

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return todo.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // セルを取得
        let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        //セルに表示する値を取得する
        cell.textLabel!.text = todo[indexPath.row]
        return cell
    }
}

まとめ

シンプルなtodoリストを作ることができました。

次はユーザーが入力できるtodoリストを作成したいと思います。

参考

参考にさせていただきました。ありがとうございます。
https://qiita.com/TD3P/items/cafa8e20029047993025

1
2
2

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
2