2017-02-23 84 views
0

我的VC中有一個代碼(作爲我的大型項目的一部分)中有以下代碼。我的一個Firebase數據庫引用被稱爲Sell_Request。它有三個孩子(name,latitudelongitude),但我現在只關注name在TableView Cell中顯示來自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

嗨,你需要重新加載你的tableview。添加你的tableview名稱的引用它的tableView,然後在你的if let數據語句的末尾添加self.tableView.reloadData() – webjunkie

+0

感謝您的回覆......我對這個還是有點新的。你的意思是創建一個變量(var tableView:UITableView!)或創建一個IBOutlet(@IBOutlet var tableView:UITableView!)或其他? – user2411290

回答

1

你只需要調用tableView.reloadData()你追加的名字到sellerUserNames陣列後,改變你的功能,這...

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()取決於你的代碼和你的表視圖的生命週期,但我已經編輯了我的代碼上面添加它在哪裏它是最li乖巧。

+0

謝謝!這似乎是這樣做的......但我似乎還有另一個問題。我得到相同的名字重複自己。我查看了一些相關的SO問題,似乎是在函數的某處調用self.sellerUserNames.removeAll()應該提供一個快速修復問題的方法,但它也只是顯示名字。 – user2411290

+0

我編輯我的答案,包括調用'sellerUserNames.removeAll()'應該解決這個問題 – MikeG

0

TableView需要數據源後,重載發生在你的代碼即sellerUserNames。所以在將名稱追加到sellerUserNames之後,您需要重新加載tableview。

在追加名稱後添加下面這行代碼。

self.tableView.reloadData()

相關問題