2017-07-19 51 views
1

我在做什麼錯誤?我知道代碼工作,因爲我從另一個視圖控制器返回時獲取單元格中的位置,但它在第一次加載應用程序時不會出現。我一直盯着這個眼睛!加載應用程序時無法獲取位置,但從另一個視圖控制器傳遞時

任何幫助表示讚賞。

下面的代碼:

import UIKit 
import CoreLocation 

class ViewController: UITableViewController { 

var arrayLocations:[[Double]] = [[27.1750199, 78.0399665]] 

var annotationAddress = "" 

override func viewDidLoad() { 
    super.viewDidLoad() 


    } 

override func viewDidAppear(_ animated: Bool) { 

    if !arrayLocations.isEmpty { 

     arrayLocations = UserDefaults.standard.array(forKey: "arrayLocations") as? [[Double]] ?? [[Double]]() 
     print("Table view is loaded") 
    } 

    self.tableView.reloadData() 

} 

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

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

    return arrayLocations.count 

} 

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

    let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Cell") 

    getLocationAddress(location: CLLocation(latitude: arrayLocations[indexPath.row][0], longitude: arrayLocations[indexPath.row][1])) 

    cell.textLabel?.text = annotationAddress 

    return cell 

} 

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

    // activeRow = indexPath.row 

    performSegue(withIdentifier: "toMapViewController", sender: nil) 

} 

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 

    if segue.identifier == "toMapViewController" { 

     let mapViewController = segue.destination as! MapViewController 

     mapViewController.arrayLocations = arrayLocations 

    } 
} 

func getLocationAddress(location: CLLocation) { 

    CLGeocoder().reverseGeocodeLocation(location) { (placemarks, error) in 

     if error != nil { 

      print(error!) 

     } else { 

      if let placemark = placemarks?[0] { 

       var address = "" 


       if placemark.subThoroughfare != nil { 

        address += placemark.subThoroughfare! + " " 

       } 

       if placemark.thoroughfare != nil { 

        address += placemark.thoroughfare! 

       } 

       self.annotationAddress = address 

       if self.annotationAddress.isEmpty { 

        self.annotationAddress = "Lat: \(location.coordinate.latitude) and Lon: \(location.coordinate.longitude)" 

       } 
      } 
     } 
    } 
} 

回答

1

變化的else塊調用reloadData如下:

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

    let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Cell") 

    getLocationAddress(location: CLLocation(latitude: arrayLocations[indexPath.row][0], longitude: arrayLocations[indexPath.row][1]), cell : cell) 


    return cell 

} 

細胞加入一個參數getLocationAddress功能

func getLocationAddress(location: CLLocation, cell: UITableViewCell) { 

    CLGeocoder().reverseGeocodeLocation(location) { (placemarks, error) in 

     if error != nil { 

      print(error!) 

     } else { 

      if let placemark = placemarks?[0] { 

       var address = "" 


       if placemark.subThoroughfare != nil { 

        address += placemark.subThoroughfare! + " " 

       } 

       if placemark.thoroughfare != nil { 

        address += placemark.thoroughfare! 

       } 

       self.annotationAddress = address 

       if self.annotationAddress.isEmpty { 

        self.annotationAddress = "Lat: \(location.coordinate.latitude) and Lon: \(location.coordinate.longitude)" 

       } 
        cell.textLabel?.text = annotationAddress 
      } 
     } 
    } 
} 
+0

太棒了,謝謝!工作過一種享受!現在我只需要弄清楚它爲什麼能起作用;;) – Aecasorg

+0

它是因爲reverseGeocodeLocation異步調用而工作的。 – KKRocks

+0

標記爲接受,如果您的問題與我的答案解決, – KKRocks

1

方法reverseGeocodeLocation是一個異步調用。這意味着當您的cellForRowAtIndexPath方法調用getLocationAddress時,它會繼續並顯示單元格。當reverseGeocodeLocation回來響應,沒有人告訴表重新加載。然後,您轉移到另一個視圖並返回,您的viewDidAppear調用reloadData導致單元格顯示值。另外需要注意的是,在這個例子中,你的單元格顯示的是前一個請求(在你移動到下一個viewController之前)而不是當前的值(是的!它每次調用cellForRowAtIndexPath時都會發送另一個請求)。

您可以使用一個Promise庫來解決這個問題,或者在你完成處理

+0

感謝您的解釋!幫助很多。 – Aecasorg

+0

嚴......完成處理程序? – Aecasorg

+0

當異步調用完成時,通常會調用「完成處理程序」。在代碼中的CLGeocoder()。reverseGeocodeLocation(location)'後面的花括號中包含的代碼塊是一個傳遞給該方法的「完成塊」,因此該方法在完成時知道該做什麼。 – Malik

0

您的方法getLocationAddress應該返回一些值,這會給你的單元格標籤一些價值,或者你可以重新加載表格getLocationAddress

+0

它確實。它更新了annotationAddress變量。我沒有設法返回一個值沒有拋出一個錯誤,所以這是最簡單的解決方案。 – Aecasorg

+0

太棒了!如果它對你有效:)你可以接受這個答案 –

相關問題