Created
November 19, 2016 09:20
-
-
Save takoikatakotako/3be7c007819dc477010b443850fa47eb to your computer and use it in GitHub Desktop.
2つのグループで構成されたUITableView
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
import UIKit | |
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { | |
// Tableに表示する文字列 | |
private var fruitItems: NSArray! = [] | |
private var vegetableItems: NSArray! = [] | |
// Sectionのタイトル | |
private var sectionTitle: NSArray! = [] | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
//TableViewに使用する配列の設定 | |
fruitItems = ["りんご", "すいか", "もも", "さくらんぼ"] | |
vegetableItems = ["キャベツ", "だいこん", "にんじん"] | |
sectionTitle = ["くだもの", "野菜"] | |
// Status Barの高さを取得を.する. | |
let statusBarHeight: CGFloat = UIApplication.shared.statusBarFrame.size.height | |
// Viewの高さと幅を取得する. | |
let viewWidth: CGFloat = self.view.frame.width | |
let viewHeight: CGFloat = self.view.frame.height | |
// TableViewの生成( status barの高さ分ずらして表示 ). | |
let myTableView: UITableView = UITableView(frame: CGRect(x: 0, y: statusBarHeight, width: viewWidth, height: viewHeight - statusBarHeight)) | |
// Cell名の登録をおこなう. | |
myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell") | |
// DataSourceの設定をする. | |
myTableView.dataSource = self | |
// Delegateを設定する. | |
myTableView.delegate = self | |
// Viewに追加する. | |
self.view.addSubview(myTableView) | |
} | |
//セクションの数を設定する | |
func numberOfSections(in tableView: UITableView) -> Int { | |
return sectionTitle.count | |
} | |
//セクションのタイトルを設定する | |
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { | |
return sectionTitle[section] as? String | |
} | |
//セル画押された時に呼ばれる | |
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { | |
print(String(indexPath.section) + "セクション目の" + String(indexPath.row) + "行目が押された") | |
if indexPath.section == 0 { | |
print("Value: \(fruitItems[indexPath.row])") | |
} else if indexPath.section == 1 { | |
print("Value: \(vegetableItems[indexPath.row])") | |
} | |
} | |
//テーブルのせるの数の設定 | |
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
if section == 0 { | |
return fruitItems.count | |
} else if section == 1 { | |
return vegetableItems.count | |
} else { | |
return 0 | |
} | |
} | |
//セルの中身のテキストを設定する。 | |
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) | |
if indexPath.section == 0 { | |
cell.textLabel?.text = "\(fruitItems[indexPath.row])" | |
} else if indexPath.section == 1 { | |
cell.textLabel?.text = "\(vegetableItems[indexPath.row])" | |
} | |
return cell | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment