【Swift】UITableView | ポケットリファレンス、サンプル付き (Xcode)
UITableView サンプル
サンプルで動作を確認 コピーペーストで確認できます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
import UIKit // テーブルビューを利用するために、UITableViewDelegate、UITableViewDataSource プロトコルを実装 class ViewController: UIViewController , UITableViewDelegate, UITableViewDataSource { // テーブルに表示する情報用 private var tableDatas : [String] = [] // テーブルビューを作成 private var uiTableView : UITableView = UITableView() override func viewDidLoad() { super.viewDidLoad() // 表示用テストデータを作成 for var i = 0; i < 100; ++i { tableDatas.append("TEST " + String(i)) } // テーブルの表示位置と大きさを設定 uiTableView.frame = CGRectMake( 0 , 0 , self.view.frame.width , self.view.frame.height ) // データソース用のデリゲートを設定 uiTableView.dataSource = self // テーブル用のデリゲートを設定 uiTableView.delegate = self // テーブルの全セルの高さを設定 uiTableView.rowHeight = 100 // セル間の区切りのスタイルを設定 uiTableView.separatorStyle = UITableViewCellSeparatorStyle.SingleLine // UITableViewCellSeparatorStyle.None // UITableViewCellSeparatorStyle.SingleLine // UITableViewCellSeparatorStyle.SingleLineEtched // セル間の区切りの色を設定 uiTableView.separatorColor = UIColor.redColor() // セルの選択可能の有無 uiTableView.allowsSelection = true // ヘッダーへ設定するビューを作成 let uiLableHeader : UILabel = UILabel() uiLableHeader.frame = CGRectMake( 0 , 0 , 200 , 30 ) uiLableHeader.text = "TableHeaderView" // ヘッダーへビューを設定 uiTableView.tableHeaderView = uiLableHeader // フッターへ設定するビューを作成 let uiLableFooter : UILabel = UILabel() uiLableFooter.frame = CGRectMake( 0 , 0 , 200 , 30 ) uiLableFooter.text = "TableFooterView" // フッターへビューを設定 uiTableView.tableFooterView = uiLableFooter // テーブルが編集可能状態を設定 uiTableView.editing = true // テーブルがバウンド可能の有無 uiTableView.bounces = true // 再利用する Cell の ID を設定 uiTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell") // ビューへテーブルをセットします。 self.view.addSubview(uiTableView) } // ■ UITableViewDataSource デリゲート // セクション内に表示する行数を設定 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return tableDatas.count } // 表示する内容を作成 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { // 再利用するセルを取得 let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) // セルのデフォルトのテキストラベルの設定 cell.textLabel!.text = tableDatas[indexPath.row] cell.textLabel!.textColor = UIColor.blueColor() cell.textLabel!.font = UIFont.systemFontOfSize(12) cell.textLabel!.textAlignment = NSTextAlignment.Left // セルのデフォルトの詳細用テキストラベルの設定 cell.detailTextLabel!.text = "ラベルテキスト" cell.detailTextLabel!.textColor = UIColor.yellowColor() cell.detailTextLabel!.font = UIFont.systemFontOfSize(12) cell.detailTextLabel!.textAlignment = NSTextAlignment.Right // セルのデフォルトの画像を設定 cell.imageView!.image = UIImage(named: "image.png") // セルのデフォルトのアクセサリーを設定 cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator // UITableViewCellAccessoryType.None // UITableViewCellAccessoryType.DisclosureIndicator // UITableViewCellAccessoryType.DetailDisclosureButton // UITableViewCellAccessoryType.Checkmark // UITableViewCellAccessoryType.DetailButton // セル選択時のスタイルを設定 cell.selectionStyle = UITableViewCellSelectionStyle.Blue // UITableViewCellSelectionStyle.None // UITableViewCellSelectionStyle.Blue // UITableViewCellSelectionStyle.Gray // UITableViewCellSelectionStyle.Default return cell } // セクションの数を設定 func numberOfSectionsInTableView(tableView: UITableView) -> Int { return tableDatas.count } // ヘッダーセクションのタイトルを設定 func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return "ヘッダータイトル" } // フッターセクションのタイトルを設定 func tableView(tableView: UITableView, titleForFooterInSection section: Int) -> String? { return "フッタータイトル" } // テーブルのセル修正完了時に呼ばれる func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { print("セル修正完了") } // テーブルのセル移動完了時に呼ばれる func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath indexPath: NSIndexPath) { print("セル移動完了") } // ■ UITableViewDelegate デリゲート // セルの高さを個別に設定、 func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { // セルが作成されるたびに呼ばれます。 // セルの行が2行目の場合 if indexPath.row == 2 { return 100 } return 50 } // ヘッダーのセクションの高さを個別に設定 func tableView(tableView: UITableView, heightForHeaderInSection section: NSInteger) -> CGFloat { // 2個めのセクションの場合 if section == 2 { return 100 } return 50 } // アクセサリーボタンタップ時に呼ばれる func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) { print("アクセサリーボタンタップ") } // セルタップ時に呼ばれる func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { print("\(indexPath.row) : 行目がタップされました。") print("\(tableDatas[indexPath.row]) : を タップ!") } } |
UITableView 概要
テーブルビューを利用時に使用します。
テーブルビューセルの区切りの種類
■ UITableViewCellSeparatorStyle
1 2 3 |
UITableViewCellSeparatorStyle.None UITableViewCellSeparatorStyle.SingleLine UITableViewCellSeparatorStyle.SingleLineEtched |
セルのデフォルトのアクセサリーの種類
■ UITableViewCellAccessoryType
1 2 3 4 5 |
UITableViewCellAccessoryType.None UITableViewCellAccessoryType.DisclosureIndicator UITableViewCellAccessoryType.DetailDisclosureButton UITableViewCellAccessoryType.Checkmark UITableViewCellAccessoryType.DetailButton |
セル選択時のスタイルの種類
■ UITableViewCellSelectionStyle
1 2 3 4 |
UITableViewCellSelectionStyle.None UITableViewCellSelectionStyle.Blue UITableViewCellSelectionStyle.Gray UITableViewCellSelectionStyle.Default |