【swift】UITableViewひな型(私用)
最近僕がつくってる画面はほぼほぼテーブルビューをつかってるので、毎回過去のコードをコピーして不要なとこを削除してっていう作業を毎回しています。
なにげにこれだけでも10分くらいかかってるかも、、、
ということで自分用ですが、ひな型です
テーブルを表示して、セルを選択すると詳細画面を開くっていうありがちな画面です。
実験とかするときによくこの形のコードつくるので
AppDelegate.swift
class AppDelegate: UIResponder, UIApplicationDelegate {
var navigationController: UINavigationController?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let viewController: ViewController = ViewController()
navigationController = UINavigationController(rootViewController: viewController)
self.window = UIWindow(frame: UIScreen.mainScreen().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.sharedApplication().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.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
tableView.delegate = self
tableView.dataSource = self
self.view.addSubview(tableView)
}
// テーブルセルの高さをかえします
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 60
}
// テーブルの行数をかえします
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.tableData.count
}
// テーブルセルにデータをセットします
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
return cell
}
// テーブルセル選択時の処理を記述します
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let viewController: DetailViewController = DetailViewController()
self.navigationController?.pushViewController(viewController, animated: true)
}
}DetaileViewController.swift
class DetailViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.whiteColor()
}
}ちょっとは仕事早くなるといいな、、、以上です