2017-02-08 180 views
0

此應用程序允許騎手請求騎車和司機接受請求。在這張桌子裏,是騎手(2)要求的騎行。TableViewController不更新單元格

無法更新tableviewcells,任何幫助將不勝感激

import UIKit 
import Firebase 
import FirebaseDatabase 
import FirebaseAuth 
import FirebaseCore 
import CoreLocation 

class RequestsTVC: UITableViewController { 

var geoCoder : CLGeocoder? 

var rideRequests = [FIRDataSnapshot]() 
let ref = FIRDatabase.database().reference() //(withPath: "RideRequests") 

override func viewDidLoad() { 
    super.viewDidLoad() 

    tableView.delegate = self 
    tableView.dataSource = self 

    self.geoCoder = CLGeocoder() 

    self.navigationController?.isNavigationBarHidden = false 

    /* 
    .observe is called whenever anything changes in the Firebase - 
    It's also called here in viewDidLoad(). 
    It's always listening. 

    */ 

    ref.child("drivers").child("RideRequests").observe(FIRDataEventType.value, with: { snapshot in 
     self.rideRequests.removeAll() 
     for item in snapshot.children{ 
      self.rideRequests.append(item as! FIRDataSnapshot) 
     } 
     self.rideRequests.reverse() 
     self.tableView.reloadData() 
    }) 

    DispatchQueue.main.async (execute: {() -> Void in 
     self.tableView.reloadData() 
    }) 

} // func viewDidLoad() 


override func viewDidAppear(_ animated: Bool) { 
    super.viewDidAppear(true) 


} 


// MARK: - Table view data source 

override func numberOfSections(in tableView: UITableView) -> Int { 

    return 1 
} 


override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    return rideRequests.count 
} 


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCell(withIdentifier: "RequestsCell", for: indexPath) 

    let usernameLabel = cell.contentView.viewWithTag(2) as! UILabel 
    let locationLbl = cell.contentView.viewWithTag(1) as! UILabel 
    let destinationLbl = cell.contentView.viewWithTag(3) as! UILabel 

    let request = self.rideRequests[indexPath.row] 

    let username = request.childSnapshot(forPath: "username") 

    let destLatString = request.childSnapshot(forPath: "destLat").value 
    let destLat = Double(destLatString as! String) 

    let destLongString = request.childSnapshot(forPath: "destLong").value 
    let destLong = Double(destLongString as! String) 


    let usernameText = username.value as! String 


    let location = RiderVC.instance.locationManager.location 
    let destination = CLLocation(latitude: destLat!, longitude: destLong!) 


    usernameLabel.text = usernameText 

    locationLbl.text = "Waiting ..." 
    destinationLbl.text = "Loading ... " 

    // LOCATION and DESTINATION 


    geoCoder?.reverseGeocodeLocation(location!, completionHandler: { (data, error) -> Void in 
     guard let placeMarks = data as [CLPlacemark]! else { 
      return 
     } 

     let loc: CLPlacemark = placeMarks[0] 
     let addressDict : [NSString: NSObject] = loc.addressDictionary as! [NSString: NSObject] 
     let addrList = addressDict["FormattedAddressLines"] as! [String] 
     print(addrList) 
     locationLbl.text = addrList[0] 

     // THAT'S THE FIRST BIT DONE 
     // THIS IS STILL INSIDE THE COMPELTION HANDLER 

     // NOW DO DESTINATION 
     // DESTINATION 


     self.geoCoder?.reverseGeocodeLocation(destination, completionHandler: { (data, error) -> Void in 

      guard let placeMarks = data as [CLPlacemark]! else { 
       return 
      } 

      let loc: CLPlacemark = placeMarks[0] 
      let addressDict : [NSString: NSObject] = loc.addressDictionary as! [NSString: NSObject] 
      let addrList = addressDict["FormattedAddressLines"] as! [String] 
      destinationLbl.text = addrList[0] 
     }) 

    }) 

    return cell 
} 

**表視圖的屏幕快照第一小區後不更新**

enter image description here

回答

1

請確保您在主隊列with parameter closure中重新加載tableview。

ref.child("drivers").child("RideRequests").observe(FIRDataEventType.value, with: { snapshot in 
    self.rideRequests.removeAll() 
    for item in snapshot.children{ 
     self.rideRequests.append(item as! FIRDataSnapshot) 
    } 
    self.rideRequests.reverse() 
    DispatchQueue.main.async (execute: {() -> Void in 
     self.tableView.reloadData() 
    }) 
}) 
0

從地理編碼器的回調異步和你不能簡單地更改表格單元格的值更新回調中的標籤值 - 您必須通知表視圖重新加載單元格以更新它。

因此,您需要從tableView:cellForRowAt中取出GeoCoder查找並將它們移到viewDidLoad或viewWillAppear中的循環中。最終值應該成爲你模型的一部分。

在GeoCoder的回調函數中,您可以通過表視圖重新載入每個調用tableView.reloadRows的單個值單元值。

+0

你有什麼可以給我一個你的意思嗎? – LizG