ListViewController.swift
import UIKit
class ListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
let dataList: Array = ["01","02"]
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self;
tableView.dataSource = self;
// Cell名の登録をおこなう.
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
// Do any additional setup after loading the view.
}
//追加③ セルの個数を指定するデリゲートメソッド(必須)
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataList.count
}
//追加④ セルに値を設定するデータソースメソッド(必須)
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// セルを取得する
let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
// セルに表示する値を設定する
cell.textLabel!.text = dataList[indexPath.row]
return cell
}
}
ListViewController.xib
tableViewを配置し、ListViewController.swift
とOutlet接続を行ってください。
ListViewController.swift
の@IBOutlet weak var tableView: UITableView!
※ListViewController.swift
でコードで生成行う場合は.xibファイル
自体不要です。