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

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

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

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

Swift

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

Q&A

0回答

619閲覧

【swift】UICollectionView内のCellにfirebaseから取得したデータを反映させたい

bunkei_enjineer

総合スコア0

Firebase

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

Swift

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

0グッド

0クリップ

投稿2021/09/20 04:11

前提・実現したいこと

UICollectionView内のCellに、他関数にて取得したデータを表示させたい

swiftで自作アプリケーションを作成しています。
firebaseに登録したデータを取得して、UICollectionView内のCellにそのデータを反映させたいです。

どうも、「cellForItemAt」が「firebaseからデータ取得する関数」より早く作動してしまい、うまく取得したデータをCellに表示できないようです。

解決方法をご教示願います。

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

Thread 1: Fatal error: Index out of range

該当のソースコード

import UIKit import Firebase class Home_Home_2_ViewController: UIViewController,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{ @IBOutlet var collectionView: UICollectionView! var dataSet: [[String]] = [] var myindustry: String! = "" override func viewDidLoad() { super.viewDidLoad() getrecommend() collectionView.delegate = self collectionView.dataSource = self let layout = UICollectionViewFlowLayout() layout.scrollDirection = .horizontal collectionView.collectionViewLayout = layout } func getrecommend() { let userID = Auth.auth().currentUser!.uid let docRef = Firestore.firestore().collection("users").document(userID).addSnapshotListener { (snapShot, error) in self.dataSet = [] if error != nil { return } self.myindustry = (snapShot?.get("industry") as! String) print(self.myindustry!) let query = Firestore.firestore().collection("talkerPost").whereField("industry1", isEqualTo: self.myindustry!).getDocuments() { (querySnapshot, err) in if let err = err { print("Error getting documents: (err)") } else { for document in querySnapshot!.documents { print("(document.documentID) => (document.data())") //ここにtableViewにどんどん値入れていく処理入れる let data = document.data() let title:String = data["talkTitle"] as! String //let name = data[""] let price:String = data["price"] as! String + "円" self.dataSet.append([title,price]) //print(name!) print(self.dataSet) print(self.dataSet[0][0]) self.collectionView.reloadData() } } } } } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 3 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "recommendationCell", for: indexPath) let reccomendtitleLabel = cell.viewWithTag(2) as! UITextView reccomendtitleLabel.text = dataSet[indexPath.row][0] let recommendpriceLabel = cell.viewWithTag(3) as! UILabel recommendpriceLabel.text = dataSet[indexPath.row][1] return cell } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { let width: CGFloat = UIScreen.main.bounds.width/3 let height = width return CGSize(width: width, height: height) } }

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問