Firebaseから配列を取得して、その配列によってテーブルビューのセルの数や内容を更新したいと思っているのですが、どのタイミングで変数を更新したらいいのでしょうか。以下のプログラムでは配列がhogearrayに代入はされるのですが、テーブルビューの更新ができません。
よろしくお願いします。
<編集>
以下のようにコードを編集しました。
Swift4
1import Foundation 2 import UIKit 3 import Firebase 4 5 class OrderListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{ 6 // インスタンス変数 7 var DBRef:DatabaseReference! 8 var hogearray : [String] = [] 9 var array1 : [String] = [] 10 11 @IBOutlet weak var tableView: UITableView! 12 13 14 15 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 16 17 return self.hogearray.count 18 } 19 20 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 21 let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) 22 cell.textLabel!.text=self.hogearray[indexPath.row] 23 return cell 24 } 25 26 override func viewDidLoad() { 27 super.viewDidLoad() 28 //インスタンスを作成 29 DBRef = Database.database().reference() 30 31 //繰り返し 32 Timer.scheduledTimer( //TimerクラスのメソッドなのでTimerで宣言 33 timeInterval: 0.5, //処理を行う間隔の秒 34 target: self, //指定した処理を記述するクラスのインスタンス 35 selector: #selector(self.newArray(_:)), //実行されるメソッド名 36 userInfo: nil, //selectorで指定したメソッドに渡す情報 37 repeats: true //処理を繰り返すか否か 38 ) 39 } 40 41 @objc func newArray(_ sender: Timer) { //(_ sender: Timer) Timerクラスのインスタンスを受け取る 42 self.hogearray = [] 43 let defaultPlace = DBRef.child("table/orderorder") 44 defaultPlace.observe(.value, with: { snapshot in 45 for item in (snapshot.children) { 46 let snapshot = item as! DataSnapshot 47 let dict = snapshot.value as! String 48 self.hogearray.append(dict) 49 } 50 51 }) 52 } 53 54 override func didReceiveMemoryWarning() { 55 super.didReceiveMemoryWarning() 56 } 57} 58
しかしまだセルが更新できません。そこで変数を追って見たところ、newArrayメソッドの中で以下の位置でprintするとhogearrayが取得できている(例:[1,2,3])ようなのですが、
Swift4
1@objc func newArray(_ sender: Timer) { //(_ sender: Timer) Timerクラスのインスタンスを受け取る 2 self.hogearray = [] 3 let defaultPlace = DBRef.child("table/orderorder") 4 defaultPlace.observe(.value, with: { snapshot in 5 for item in (snapshot.children) { 6 let snapshot = item as! DataSnapshot 7 let dict = snapshot.value as! String 8 self.hogearray.append(dict) 9 } 10 print (self.hogearray) 11 }) 12 13 }
以下の位置では"[]"が出力されます。
Swift4
1@objc func newArray(_ sender: Timer) { //(_ sender: Timer) Timerクラスのインスタンスを受け取る 2 self.hogearray = [] 3 let defaultPlace = DBRef.child("table/orderorder") 4 defaultPlace.observe(.value, with: { snapshot in 5 for item in (snapshot.children) { 6 let snapshot = item as! DataSnapshot 7 let dict = snapshot.value as! String 8 self.hogearray.append(dict) 9 } 10 11 }) 12 print (self.hogearray) 13 }
メソッド外で変数が共有できていないということになると思うのですが、どこに原因があるのでしょうか。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/09/12 05:24
2018/09/12 05:32
2018/09/12 06:28
2018/09/12 06:48
2018/09/12 07:19
2018/09/12 07:29
2018/09/12 07:44
2018/09/12 07:51
2018/09/12 07:58
2018/09/12 08:04
2018/09/12 09:47
2018/09/12 10:41 編集
2018/09/12 11:02 編集
2018/09/12 11:25
2018/09/12 12:30 編集
2018/09/12 16:34