tableViewに複数のセクションを設定して、セクション毎にデータを表示させたいのですが、現状1番目のセクション(timesの配列)しか表示することができません。
解決方法をご教示いただけませんでしょうか。
viewController
1import UIKit 2 3class tableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 4 5 let times = ["10時", "11時", "12時"] 6 let others = ["その他", "そのた"] 7 let tests = ["test"] 8 let sections = ["時間", "その他", "test"] 9 10 override func viewDidLoad() { 11 super.viewDidLoad() 12 } 13// セクションの数を返す 14 func numberOfSectionsInTableView(tableView: UITableView) -> Int { 15 return sections.count 16 } 17 18// セクションのタイトルを返す 19 func tableView(_ tableView: UITableView,titleForHeaderInSection section: Int) -> String? { 20 return sections[section] as? String 21 } 22 23// テーブルに表示する配列の総数を返す 24 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 25 if section == 0 { 26 return times.count 27 } else if section == 1 { 28 return others.count 29 } else if section == 2 { 30 return tests.count 31 } else { 32 return 0 33 } 34 } 35 36 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 37 let cell = tableView.dequeueReusableCell(withIdentifier: "tableViewCell", for: indexPath) 38 if indexPath.section == 0 { 39 cell.textLabel!.text = times[indexPath.row] 40 } else if indexPath.section == 1 { 41 cell.textLabel!.text = others[indexPath.row] 42 } else if indexPath.section == 2 { 43 cell.textLabel!.text = tests[indexPath.row] 44 } 45 return cell 46 }
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。