実装したいこと
firebaseを用いて、firedatabaseに保存した値をtableviewに表示したいです。
問題点
firedatabase内には3つの異なる値がはいっているのですが、ビルドしてみると一番最初の値が3回表示されてしまいます。
例えば、firedatabase内にはA,B,Cという3つの値があるのですが、シュミレーターを動かしてみるとA,A,Aと1つの値が3回tableviewに表示されています。
swift
1 //---↓FireDBの読み込みメソッド↓--- 2 func readFireDB() { 3 4 ref.child(uid!).child("TestInformation").observe(.value, with:{(snapShots) in 5 if snapShots.children.allObjects is [DataSnapshot] { 6 print("the amount of snapshots... (snapShots.childrenCount)") 7 print("the content of snapshots... (snapShots)") 8 self.snap = snapShots 9 } 10 self.reload(snap: self.snap) 11 }) 12 } 13 14 func reload(snap: DataSnapshot) { 15 if snap.exists() { 16 print("(snap)") 17 testInfoArray.removeAll() 18 for item in snap.children { 19 testInfoArray.append(item as! DataSnapshot) 20 print(testInfoArray) 21 } 22 ref.child(uid!).keepSynced(true) 23 testInfoTableView.reloadData() 24 } 25 } 26 //---↑ここまで↑--- 27 28 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 29 30 let cell = testInfoTableView.dequeueReusableCell(withIdentifier: "TestInfoCell") as! TestInfoTableViewCell 31 cell.clipsToBounds = true 32 33 let testInformation = testInfoArray[indexPath.row] 34 print("the content of testInformation... (String(describing: testInformation.value))") 35 let testInfoContent = testInformation.value as! Dictionary<String, Any> 36 cell.classNameLabel.text = testInfoContent["class"] as? String 37 cell.roomNameLabel.text = testInfoContent["room"] as? String 38 cell.teacherNameLabel.text = testInfoContent["teacher"] as? String 39 cell.yearLabel.text = testInfoContent["year"] as? String 40 cell.termLabel.text = testInfoContent["term"] as? String 41 print("the content of testInfoContent... (testInfoContent.values)") 42 return cell 43 44 }
わからないこと
ブレークポイントをおいていろいろ調べて見たところ、func tablView内のtestInfoArrayにはきちんと異なる3つの値が入ってきていました。しかし、それ以降の行では1番目の値のみしか扱われていませんでした。どうすれば2番目、3番目の値を表示させることができますか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。