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

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

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

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

Q&A

解決済

2回答

166閲覧

swift tableview(table view section)について

globalplus

総合スコア119

Swift

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

0グッド

0クリップ

投稿2019/04/13 09:39

tableviewを使い、項目ごとに分けたいと思っています。項目は4つあります。
しかし下記のコードですと最初の項目しか表示されず困っています。解決策をご存知の方、教えて下さい。よろしくお願います。

DiaryViewController

1import UIKit 2 3class DiaryViewController: UIViewController, UITableViewDelegate, UITableViewDataSource 4{ 5 @IBAction func back(_ sender: Any) 6 { 7 self.dismiss(animated: true, completion: nil) 8 } 9 10 @IBOutlet weak var tableView: UITableView! 11 12 let diaryCell:[String] = ["最初の項目", "二番目の項目", "三番目の項目", "四番目の項目"] 13 14 15 override func viewDidLoad() 16 { 17 super.viewDidLoad() 18 } 19 20 func numberOfSectionsInTableView(tableView: UITableView) -> Int 21 { 22 return diaryCell.count 23 } 24 25 func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? 26 { 27 return diaryCell[section] 28 } 29 30 31 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 32 { 33 return 1 34 } 35 36 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 37 { 38 let cell = tableView.dequeueReusableCell(withIdentifier: "1", for: indexPath) 39 return cell 40 } 41 42 43 44 /* 45 // MARK: - Navigation 46 47 // In a storyboard-based application, you will often want to do a little preparation before navigation 48 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 49 // Get the new view controller using segue.destination. 50 // Pass the selected object to the new view controller. 51 } 52 */ 53 54} 55

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

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

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

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

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

guest

回答2

0

ベストアンサー

上記の回答を修正致します。
質問者様のコードの変更点を最小限にした回答は以下の通りです。

修正前

swift

1func numberOfSectionsInTableView(tableView: UITableView) -> Int

修正後

swift

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

この部分を直すだけで全項目が表示されます。

投稿2019/04/13 14:16

BMJr

総合スコア80

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

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

globalplus

2019/04/24 04:33

ご回答ありがとうございます。 無事表示されました。
guest

0

numberOfSectionsInTableView を 1 にして、numberOfRowsInSection を diaryCell.count とし、cell.textLabel?.text = diaryCell[indexPath.row] として以下のように書いてみました。

swift

1import UIKit 2 3class DiaryViewController: UIViewController, UITableViewDelegate, UITableViewDataSource 4{ 5 @IBAction func back(_ sender: Any) 6 { 7 self.dismiss(animated: true, completion: nil) 8 } 9 10 @IBOutlet weak var tableView: UITableView! 11 12 let diaryCell:[String] = ["最初の項目", "二番目の項目", "三番目の項目", "四番目の項目"] 13 14 15 override func viewDidLoad() 16 { 17 super.viewDidLoad() 18 } 19 20 func numberOfSectionsInTableView(tableView: UITableView) -> Int 21 { 22 return 1 23 } 24 25 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 26 { 27 return diaryCell.count 28 } 29 30 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 31 { 32 let cell = tableView.dequeueReusableCell(withIdentifier: "1", for: indexPath) 33 cell.textLabel?.text = diaryCell[indexPath.row] 34 return cell 35 } 36 37}

投稿2019/04/13 12:29

BMJr

総合スコア80

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問