[swift] tableViewControllerのテンプレート
tableViewControllerのテンプレートを記述しておく。
StoryboardにViewController + TableView + TableViewCellを配置しておくこと。
TableViewCellには”tableCell”という名前を振っておく
import Foundation
import UIKit
class testTableViewContoller: UIViewController, UITableViewDelegate, UITableViewDataSource{
@IBOutlet weak var tableView: UITableView!
let array:[String] = ["swift","Ruby","Rails","PHP","HTML.CSS","JS","Java"]
override func viewDidLoad() {
super.viewDidLoad()
// Delegateの設定
tableView.dataSource = self
tableView.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// 1sectionにおけるセルの数
return array.count
}
func numberOfSections(in tableView: UITableView) -> Int {
// sectionの数
return 1
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
// セルの高さ
return 50
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// セルの内容設定
let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath)
cell.textLabel?.text = array[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//選択された時の処理
}
}