先にテーブルの中身が設定されてしまうため、firebaseから引っ張ってきたデータを入れられません
swift
1func setupFirebase() { 2 // DatabaseReferenceのインスタンス化 3 ref = Database.database().reference() 4 ref.child("communities").observeSingleEvent(of: .value, with: { (snapshot) in 5 // Get user value 6 let value = snapshot.value as! NSDictionary 7 8 // NSDictionary -> Dictionary 9 var dic: Dictionary = value as Dictionary 10 print(dic) 11 self.titleArr.insert(dic.values, at: 0) 12 print(self.titleArr) 13// print(self.titleArr[0]["title"]) 14 15 // ... 16 }) { (error) in 17 print(error.localizedDescription) 18 } 19 } 20 21 override func viewDidLoad() { 22 super.viewDidLoad() 23 setupFirebase() 24 25 // Do any additional setup after loading the view. 26 } 27 28 29 //各セルの要素を設定する 30 func tableView(_ table: UITableView, 31 cellForRowAt indexPath: IndexPath) -> UITableViewCell { 32 33 // tableCell の ID で UITableViewCell のインスタンスを生成 34 let cell = table.dequeueReusableCell(withIdentifier: "tableCell", 35 for: indexPath) 36 // Tag番号 1 で UILabel インスタンスの生成 37 let label2 = cell.viewWithTag(1) as! UILabel 38 print(titleArr) 39 print(indexPath.row) 40// let titleDic = titleArr[indexPath.row] 41// print(titleDic) 42// let test = self.label2Array[indexPath.row] as! NSDictionary 43// let title = titleDic["title"] as! String 44// label2.text = String(describing: title) 45 46 return cell 47 }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。