質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

1回答

961閲覧

【swift】tableViewでセクションを複数表示させたいのですが…

runban

総合スコア152

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2022/07/23 04:47

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 }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

swift

1func numberOfSectionsInTableView(tableView: UITableView) -> Int {

ではなくて、

swift

1func numberOfSections(in tableView: UITableView) -> Int {

ですね。
これが定義されていないため、セクション数がデフォルトの1になっています。

ちなみに、上の書き方はSwift2時代のものです。

cf. numberOfSections(in:)

投稿2022/07/28 07:50

fuzzball

総合スコア16731

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問