Swift4.2 UITableViewひな型
コピペ用
AppDelegate.swift
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var navigationController: UINavigationController?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let viewController: ViewController = ViewController()
navigationController = UINavigationController(rootViewController: viewController)
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
return true
}
・・・
}
ViewController.swift
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var tableView: UITableView!
var tableData: [Dictionary<String,AnyObject>]!
override func viewDidLoad() {
super.viewDidLoad()
// データを初期化します
tableData = [Dictionary<String,AnyObject>]()
// VIEWをセットします
setView()
}
// VIEWをセットします
func setView() {
let statusBarHeight: CGFloat = UIApplication.shared.statusBarFrame.height
let displayWidth = self.view.frame.width
let displayHeight = self.view.frame.height
tableView = UITableView(frame: CGRect(x:0, y:statusBarHeight, width:displayWidth, height:displayHeight))
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
tableView.delegate = self
tableView.dataSource = self
self.view.addSubview(tableView)
}
// テーブルセルの高さをかえします
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 60
}
// テーブルの行数をかえします
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.tableData.count
}
// テーブルセルにデータをセットします
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath)
return cell
}
// テーブルセル選択時の処理を記述します
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let viewController: DetailViewController = DetailViewController()
self.navigationController?.pushViewController(viewController, animated: true)
}
}DetailViewController.swift
import UIKit
class DetailViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.white
}
}以上