2017-02-23 45 views
0

私のVC(私の大きなプロジェクトの一部として)に次のコードがあります。 Firebase DBリファレンスの1つはSell_Requestです。それは3人の子供(name,latitudelongitude)を持っていますが、私は今のところnameにしか関心がありません。テーブルビューのセルでFirebaseからデータを表示

私はそれを文字列配列に追加しようとしており、コンソールにも印刷しています。コンソールに名前が表示されていますが、個々のセルに名前が表示されません。

ありがとうございます。

import UIKit 
import Firebase 
import FirebaseDatabase 
import MapKit 
import CoreLocation 

class RequestVC: UITableViewController, CLLocationManagerDelegate { 

    var locationManager = CLLocationManager() 
    var sellerUserNames = [String]() 

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     if segue.identifier == "backToMain" { 
      self.navigationController?.navigationBar.isHidden = true 
     } 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     locationManager.delegate = self 
     locationManager.desiredAccuracy = kCLLocationAccuracyBest 
     locationManager.requestWhenInUseAuthorization() 
     locationManager.startUpdatingLocation() 

    } 

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
     if let location = manager.location?.coordinate { 
      let databaseRef = FIRDatabase.database().reference() 
      databaseRef.child("Sell_Request").queryOrderedByKey().observe(.childAdded, with: { (FIRDataSnapshot) in 

       if let data = FIRDataSnapshot.value as? NSDictionary { 
        if let name = data[Constants.NAME] as? String { 
           self.sellerUserNames.append(name) 
           print(name) 
           } 
          } 
         }) 
        } 
       } 


    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 



    // MARK: - Table view data source 

    override func numberOfSections(in tableView: UITableView) -> Int { 
     // #warning Incomplete implementation, return the number of sections 
     return 1 
    } 

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     // #warning Incomplete implementation, return the number of rows 
     return sellerUserNames.count 
    } 


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 

     cell.textLabel?.text = sellerUserNames[indexPath.row] 

     return cell 
    } 


    /* 
    // Override to support conditional editing of the table view. 
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { 
     // Return false if you do not want the specified item to be editable. 
     return true 
    } 
    */ 

    /* 
    // Override to support editing the table view. 
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
     if editingStyle == .delete { 
      // Delete the row from the data source 
      tableView.deleteRows(at: [indexPath], with: .fade) 
     } else if editingStyle == .insert { 
      // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
     }  
    } 
    */ 

    /* 
    // Override to support rearranging the table view. 
    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) { 

    } 
    */ 

    /* 
    // Override to support conditional rearranging of the table view. 
    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool { 
     // Return false if you do not want the item to be re-orderable. 
     return true 
    } 
    */ 

    /* 
    // MARK: - Navigation 

    // In a storyboard-based application, you will often want to do a little preparation before navigation 
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     // Get the new view controller using segue.destinationViewController. 
     // Pass the selected object to the new view controller. 
    } 
    */ 

} 
+1

あなたのカウントが0.Aでないことを確認できますか?メインスレッドの印刷ラインリロードテーブルの後。 –

+1

こんにちは、あなたのテーブルビューをリロードする必要があります。あなたのテーブルビュー名の参照を追加してください。それから、if letデータステートメントの最後に、self.tableView.reloadData()を追加してください。 – webjunkie

+0

あなたの応答に感謝します...私はまだ少しです。変数(var tableView:UITableView!)を作成するか、IBOutlet(@IBOutlet var tableView:UITableView!)を作成することを意味しましたか? – user2411290

答えて

1

...

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    if let location = manager.location?.coordinate { 
     let databaseRef = FIRDatabase.database().reference() 

     sellerUserNames.removeAll() 
     databaseRef.child("Sell_Request").queryOrderedByKey().observe(.childAdded, with: { (FIRDataSnapshot) in 

      if let data = FIRDataSnapshot.value as? NSDictionary { 
       if let name = data[Constants.NAME] as? String { 
          self.sellerUserNames.append(name) 
          print(name) 
          self.tableView.reloadData() 
          } 
         } 
        }) 
       } 
      } 

編集

/sellerUserNames.removeAll()はあなたのコードとあなたのテーブルビューのライフサイクルに依存していますが、上記の私のコードを編集して、それが最も居る場所に追加します適切なkely。

+0

ありがとう!それはそれをするように見えました...しかし、私は別の問題を抱えているようです。私は同じ名前が繰り返されています。私はいくつかの関連する質問を見て、それは関数のどこかにself.sellerUserNames.removeAll()を呼び出すように、それがした問題に迅速な修正を提供する必要があります...しかし、それはまた、最初の名前を示しています。 – user2411290

+0

問題を解決するはずの 'sellerUserNames.removeAll()'への呼び出しを含めるように私の答えを編集しました。 – MikeG

0

TableViewデータソースは、あなたのコード内のsellerUserNamesすなわち変化を持っていた後、リロードする必要があります。したがって、名前をsellerUserNamesに追加した後、テーブルビューをリロードする必要があります。

名前を追加した後にこのコード行を追加します。

あなたはちょうどあなたが sellerUserNames配列に名前を追加した後、 tableView.reloadData()を呼び出すこれにあなたの関数を変更する必要が

self.tableView.reloadData()

関連する問題