今回やること
swift5を使用してtodoリストを作成します。
ソースとストーリーボード
こちらはストーリーボード
tableViewを置いてその上にtable cellを置いただけです。
スクリーンショット上ではわからない点としてはOutletsのdataSourceとdelegateをviewコントローラーを接続しています。
こちらが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