Created
June 27, 2016 23:32
-
-
Save chuck0523/7b17c314f965af762af69a45f928964a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// 006 | |
// | |
// Created by chuck on 6/28/16. | |
// Copyright © 2016 chuck. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { | |
// tableで使う配列 | |
private let myItems: NSArray = ["test1", "test2", "test3"] | |
private var myTableView: UITableView! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
let barHeight: CGFloat = UIApplication.sharedApplication().statusBarFrame.size.height | |
let displayWidth: CGFloat = self.view.frame.width | |
let displayHeight: CGFloat = self.view.frame.height | |
// UITableの生成。 | |
myTableView = UITableView(frame: CGRect(x: 0, y:barHeight, width: displayWidth, height: displayHeight - barHeight)) | |
// Call名の登録。 | |
myTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "MyCell") | |
myTableView.dataSource = self | |
myTableView.delegate = self | |
self.view.addSubview(myTableView) | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
// Cellが選択されたら呼び出されるデリゲートメソッド | |
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { | |
print("Num: \(indexPath.row)") | |
print("Value: \(myItems[indexPath.row])") | |
} | |
// Cellの総数を返すデータソースメソッド | |
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return myItems.count | |
} | |
// Cellに値を設定するデータソースメソッド | |
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { | |
// 再利用するCellを取得する | |
let cell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath) | |
// Cellに値を設定する | |
cell.textLabel!.text = "\(myItems[indexPath.row])" | |
return cell | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment