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

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

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

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

Q&A

解決済

1回答

488閲覧

swift。tableviewに表示される値が全て同じになってしまう。

退会済みユーザー

退会済みユーザー

総合スコア0

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

0グッド

0クリップ

投稿2018/02/17 03:58

実装したいこと

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番目の値を表示させることができますか?

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

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

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

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

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

guest

回答1

0

ベストアンサー

indexPath.sectionにすればうまくいった。

投稿2018/02/17 08:42

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問