6 UITableView

Swift 3.0 // // ViewController.swift // UIKit006 // // Copyright © 2016年 FaBo, Inc. 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() // Status Barの高さを取得する. let barHeight: CGFloat = UIApplication.shared.statusBarFrame.size.height // Viewの高さと幅を取得する. let displayWidth: CGFloat = self.view.frame.width let displayHeight: CGFloat = self.view.frame.height // TableViewの生成(Status barの高さをずらして表示). myTableView = UITableView(frame: CGRect(x: 0, y: barHeight, width: displayWidth, height: displayHeight)) // Cell名の登録をおこなう. myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell") // DataSourceを自身に設定する. myTableView.dataSource = self // Delegateを自身に設定する. myTableView.delegate = self // Viewに追加する. self.view.addSubview(myTableView) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } /* Cellが選択された際に呼び出される */ func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 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, cellForRowAt indexPath: IndexPath) -> UITableViewCell { // 再利用するCellを取得する. let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath as IndexPath) // Cellに値を設定する. cell.textLabel!.text = "\(myItems[indexPath.row])" return cell } } Swift 2.3 // // ViewController.swift // UIKit006 // // Copyright © 2016年 FaBo, Inc. 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() // Status Barの高さを取得する. let barHeight: CGFloat = UIApplication.sharedApplication().statusBarFrame.size.height // Viewの高さと幅を取得する. let displayWidth: CGFloat = self.view.frame.width let displayHeight: CGFloat = self.view.frame.height // TableViewの生成(Status barの高さをずらして表示). myTableView = UITableView(frame: CGRectMake(0, barHeight, displayWidth, displayHeight)) // Cell名の登録をおこなう. myTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "MyCell") // DataSourceを自身に設定する. myTableView.dataSource = self // Delegateを自身に設定する. myTableView.delegate = self // Viewに追加する. 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 } } 2.3と3.0の差分 CGRectMake()がCGRect()に変更.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.