Xibを使ってTableViewのCellを登録・その1
Xibを使ってTableViewのCellを登録・その2
- Xcode 8.3.3
- Swift 3.1
コードは同じのを使い回します🙇
元コードを再掲。行数3のテーブルを表示するだけのViewControllerです😀
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 | import UIKit class ParentViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { override func viewDidLoad() { super .viewDidLoad() _TableView = UITableView() _TableView?.frame = self .view.frame _TableView?.register(UINib(nibName: "ExampleTableViewCell" , bundle: nil), forCellReuseIdentifier: "ExampleIdentifier" ) _TableView?.delegate = self _TableView?.dataSource = self self .view.addSubview(_TableView) } override func didReceiveMemoryWarning() { super .didReceiveMemoryWarning() } / / データ数 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int ) - > Int { return 3 } / / セルデータを返す func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) - > UITableViewCell { var cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "ExampleIdentifier" , for : indexPath) switch indexPath.row { case 1 : cell.textLabel?.text = "1行目" case 2 : cell.textLabel?.text = "2行目" case 3 : cell.textLabel?.text = "3行目" default: break } return cell } / / セル選択 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { / / セル選択時の処理 } } |
ではここにセクションを2つ追加してみます。
一応分かりやすいようにセクション1は行数3、セクション2は行数4として設定します。
セクション設定とセクション毎のデータ設定を把握してもらえればと😀
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 | import UIKit class ParentViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { override func viewDidLoad() { super .viewDidLoad() _TableView = UITableView() _TableView?.frame = self .view.frame _TableView?.register(UINib(nibName: "ExampleTableViewCell" , bundle: nil), forCellReuseIdentifier: "ExampleIdentifier" ) _TableView?.delegate = self _TableView?.dataSource = self self .view.addSubview(_TableView) } override func didReceiveMemoryWarning() { super .didReceiveMemoryWarning() } / / セクション数 func numberOfSections( in tableView: UITableView) - > Int { return 2 } / / セクション名 func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int ) - > String? { if section = = 1 { return "セクション1だよ" } else if section = = 2 { return "セクション2になります!" } return nil } / / セクション単位のデータ数 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int ) - > Int { if section = = 1 { return 3 } else if section = = 2 { return 4 } return 0 } / / セルデータを返す func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) - > UITableViewCell { var cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "ExampleIdentifier" , for : indexPath) if indexPath.section = = 1 { switch indexPath.row { case 1 : cell.textLabel?.text = "セクション1の1行目" case 2 : cell.textLabel?.text = "セクション1の2行目" case 3 : cell.textLabel?.text = "セクション1の3行目" default: break } } else if indexPath.section = = 2 { switch indexPath.row { case 1 : cell.textLabel?.text = "セクション2の1行目" case 2 : cell.textLabel?.text = "セクション2の2行目" case 3 : cell.textLabel?.text = "セクション2の3行目" case 4 : cell.textLabel?.text = "セクション2の4行目" default: break } } return cell } / / セル選択 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { / / セル選択時の処理 } } |
通常こんなハードコーディングすることはありません。
JSON等を解析して配列にしたりして利用することとなると思います。
データ数を返すメソッドをcount使ったりすると思うので、適宜改善して頂ければと!
0 件のコメント:
コメントを投稿