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

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

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

Firebaseは、Googleが提供するBasSサービスの一つ。リアルタイム通知可能、並びにアクセス制御ができるオブジェクトデータベース機能を備えます。さらに認証機能、アプリケーションのログ解析機能などの利用も可能です。

TableView

TableView(UITableView)とは、リスト形式で表示するコントロールで、ほとんどのアプリに使用されています。画面を「行」に分けて管理し、一般的には各行をタップした際に詳細画面に移動します。

Swift

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

Q&A

0回答

780閲覧

viewDidLoadでtableviewにデータが表示できない。

Ogikubo

総合スコア1

Firebase

Firebaseは、Googleが提供するBasSサービスの一つ。リアルタイム通知可能、並びにアクセス制御ができるオブジェクトデータベース機能を備えます。さらに認証機能、アプリケーションのログ解析機能などの利用も可能です。

TableView

TableView(UITableView)とは、リスト形式で表示するコントロールで、ほとんどのアプリに使用されています。画面を「行」に分けて管理し、一般的には各行をタップした際に詳細画面に移動します。

Swift

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

0グッド

0クリップ

投稿2021/01/07 13:29

編集2021/01/07 16:08

前提・実現したいこと

viewDidLoadでFirebaseから取得したデータを表示させたい。
ログインして次のページに移動した時、移動先のページでviewDidLoadを使用し既にtableViewにデータが表示されている状態にしたい。

発生している問題・エラーメッセージ

viewDidload時にtableViewにデータが表示されていない。 (showAllDataが呼ばれていない?)

該当のソースコード

swift

1 override func viewDidLoad() { 2 super.viewDidLoad() 3 4 showAllData() 5 6 self.tableView.allowsSelection = true 7 seachBar.delegate = self 8 seachBar.showsCancelButton = true 9 seachBar.showsSearchResultsButton = true 10 seachBar.placeholder = "キーワードを入力してください" 11 12 13 } 14 15 func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { 16 17 if searchBar.text == "" { 18 showAllData() 19 }else{ 20 self.timeLines.removeAll() 21 timeLineRef.whereField("musicName", isEqualTo: seachBar.text!).getDocuments { (querySnapshot, error) in 22 if error != nil{ 23 return 24 }else{ 25 self.seachedtimeLines = querySnapshot!.documents.map { document in 26 let data = TimeLineModel(document: document) 27 28 return data 29 } 30 } 31 } 32 self.tableView.reloadData() 33 } 34 35 } 36 37 38 override func viewWillAppear(_ animated: Bool) { 39 super.viewWillAppear(animated) 40 41 showAllData() 42 } 43 44 func showAllData() { 45 timeLineRef.getDocuments { (querySnapshot,error) in 46 self.timeLines.removeAll() 47 48 if error != nil { 49 return 50 } 51 self.timeLines = querySnapshot!.documents.map { document in 52 let data = TimeLineModel(document: document) 53 self.timeLines.insert(data, at: 0) 54 return data 55 } 56 } 57 self.tableView.reloadData() 58 } 59}

swift

1 override func numberOfSections(in tableView: UITableView) -> Int { 2 return 1 3 } 4 5 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 6 if self.seachBar.text == "" { 7 return timeLines.count 8 }else{ 9 return seachedtimeLines.count 10 } 11 12 } 13 14 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 15 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TimeLineCell 16 if seachBar.text == "" { 17 cell.timeLineModel = self.timeLines[indexPath.row] 18 }else{ 19 cell.timeLineModel = self.seachedtimeLines[indexPath.row] 20 } 21 return cell 22 } 23 24 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 25 26 let selectedCell = tableView.cellForRow(at: indexPath) 27 28 if seachBar.text == ""{ 29 let selectedTimeLine = timeLines[indexPath.row] 30 performSegue(withIdentifier: "detail", sender: selectedTimeLine) 31 }else{ 32 let selectedTimeLine = seachedtimeLines[indexPath.row] 33 performSegue(withIdentifier: "detail", sender: selectedTimeLine) 34 } 35 36 } 37 38 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 39 40 if segue.identifier == "detail" { 41 42 let detailViewController = segue.destination as! DetailViewController 43 detailViewController.pushedTimeLine = sender as? TimeLineModel 44 } 45 } 46 override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 47 return 82 48 }

補足情報(FW/ツールのバージョンなど)

他の場所でshowAllDataを読んだ場合は表示できます。

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

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

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

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

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

ichi888

2021/01/07 15:28

コードを見る限り、viewDidLoadが呼ばれるタイミングでshowAllData()が実行されるはずです。 UITableViewDataSourceのメソッドを実装している箇所を添付していただいてもよろしいでしょうか?
Ogikubo

2021/01/07 16:09

ichi888さんコメントありがとうございます。 添付させていただきました!
ichi888

2021/01/08 04:54

ありがとうございます! seachedtimeLinesの要素がTableViewに表示する内容ですね。 getDocumentsの引数closureのなかでseachedtimeLinesをセットしているのですが、 closureが実行されるタイミングによっては、seachedtimeLinesに要素が追加される前に tableView.reloadが走る可能性がありますね。 試しに、`self.tableView.reloadData()`の部分を以下のように移動させてみたらどうなりますでしょうか? ``` self.seachedtimeLines = querySnapshot!.documents.map { document in let data = TimeLineModel(document: document) return data } self.tableView.reloadData() ```
ichi888

2021/01/08 05:00

別件ですが、循環参照になってます。weak referenceにして回避してください。 ``` getDocuments { [weak self] (querySnapshot, error) in // 省略 // ブロック内のselfがオプショナル型 self?.seachedtimeLines = querySnapshot!.documents.map { document in ```
Ogikubo

2021/01/08 06:20

ありがとうございます!! self.tableView.reloadData()を移動させたところ表示される様になりました! 循環参照意識したことなかったです、、ご指摘有難うございます! これを機に意識していこうと思います。 ichi888さん本当にご回答有難うございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

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

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

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問