前提・実現したいこと
Firebase/RealtimeDatabase に保存したデータを、TableViewに配列したいんですが、うまくデータベースから値を取得できません。出力イメージを下に添付しています。
データベースはこの下記のように保存しています。
データベースから、シリアルナンバー(number) でフィルターをかけて履歴をTableViewに表示したいと思っています。
発生している問題・エラーメッセージ
コードのエラーは出ないんですが、ビルドしてもシミュレータのTableViewに何も表示されません。 データベースからうまく取得できていないみたいです。
該当のソースコード
HomeViewController
1class History { 2 3 var time:String? = "" 4 var status:String? = "" 5 var price:String? = "" 6 7 init(time: String?, status: String?, price: String?) { 8 self.time = time 9 self.status = status 10 self.price = price 11 } 12 13} 14 15class HomeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 16 17** 中略 ** 18 19 var historys = [History]() 20 21 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 22 return historys.count 23 } 24 25 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 26 let cell = tableView.dequeueReusableCell(withIdentifier: "historyCell", for: indexPath) as! HomeViewControllerTableViewCell 27 let history = historys[indexPath.row] 28 cell.timeLabel.text = history.time 29 cell.statusLabel.text = history.status 30 cell.priceLabel.text = history.price 31 return cell 32 } 33 34** 中略 ** 35 36 override func viewDidLoad() { 37 super.viewDidLoad() 38 39 tableView.dataSource = self 40 tableView.delegate = self 41 42 // 遷移元のTextFieldに入力したシリアルナンバーを"argString"に値渡し 43 serialLabel.text = argString 44 45 46 //残高に表示するデータをデータベースから取り出し 47 ref = Database.database().reference().child("Action") 48 49 ref.queryOrdered(byChild: "number").queryEqual(toValue: argString).queryLimited(toLast: 1).observeSingleEvent(of: .value, with: {(snapshot) in 50 for item in snapshot.children { 51 let snap = item as! DataSnapshot 52 let dict = snap.value as! [String: Any] 53 let pricedata = dict["balance"] as! Int 54 // データの中身 55 print(pricedata as Any) 56 self.balance = pricedata 57 self.price.text = String(self.balance) + String(self.entext) 58 } 59 }) 60 61 //データベースから履歴に表示させるデータを取り出す 62 func LoadHistory() { 63 ref.queryOrdered(byChild: "number").queryEqual(toValue: argString).observeSingleEvent(of: .value, with: {(snapshot) in 64 for item2 in snapshot.children { 65 let snap2 = item2 as! DataSnapshot 66 let dict2 = snap2.value as! [String: Any] 67 let time = dict2["time"] as! String 68 let status = dict2["status"] as! String 69 let price = dict2["price"] as! String 70 let hist = History(time: time, status: status, price: price) 71 self.historys.append(hist) 72 } 73 self.tableView.reloadData() 74 }) 75 } 76 77 78 } 79 80 81 override func didReceiveMemoryWarning() { 82 super.didReceiveMemoryWarning() 83 } 84 85 86} 87
HomeViewControllerTableViewCell
1 2import UIKit 3 4class HomeViewControllerTableViewCell: UITableViewCell { 5 6 @IBOutlet weak var timeLabel: UILabel! 7 @IBOutlet weak var statusLabel: UILabel! 8 @IBOutlet weak var priceLabel: UILabel! 9 10 override func awakeFromNib() { 11 super.awakeFromNib() 12 // Initialization code 13 } 14 override func setSelected(_ selected: Bool, animated: Bool) { 15 super.setSelected(selected, animated: animated) 16 } 17 18} 19
試したこと
データベースからうまくデータを取得できてるか確認するために、「//データベースから履歴に表示させるデータを取り出す」の部分でprint()コードを記述してみたんですが、とくになにも出力されませんでした(汗)
補足情報(FW/ツールのバージョンなど)
Swift5
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/23 08:40
2019/08/23 08:44
2019/08/23 08:48
2019/08/23 08:52
2019/08/23 08:54
2019/08/23 08:57
2019/08/23 09:11
2019/08/23 09:13
2019/08/23 09:31
2019/08/23 09:34
2019/08/23 10:23
2019/08/23 10:38
2019/08/23 11:07
2019/08/23 11:07
2019/08/23 11:13
2019/08/23 11:15 編集
2019/08/23 11:34
2019/08/23 11:41