我做错了什么?我知道代码有效,因为我从另一个视图控制器返回时获取单元格中的位置但是当应用程序第一次加载时它不会出现。我一直盯着自己看这个!
任何帮助表示感谢。
以下是代码:
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)"
}
}
}
}
}
答案 0 :(得分:1)
方法reverseGeocodeLocation
是异步调用。这意味着,当您的cellForRowAtIndexPath
方法调用getLocationAddress
时,它会继续显示并显示该单元格。当reverseGeocodeLocation
返回响应时,没有人告诉表重新加载。然后,您转移到另一个视图并返回,viewDidAppear
调用reloadData
,使单元格显示值。这里要注意的另一件事是,在这个例子中,你的单元格显示上一个请求的值(在你移动到下一个viewController之前)而不是当前的值(是的!它每次调用cellForRowAtIndexPath
时都会发送另一个请求)。
您可以使用Promise
库来解决此问题,也可以在完成处理程序的reloadData
块中调用else
答案 1 :(得分:1)
更改如下:
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
}
}
}
}
答案 2 :(得分:0)
您的方法.htaccess
应该返回一些可以为您的单元格标签赋值的值,或者您可以在getLocationAddress
中重新加载表格