我有两个为我提供数据的服务,第一个为我提供经度和纬度。我将它们发送到谷歌位置服务以获取“formatted_address”,然后将此格式化地址填充到 TableView
func getData(){
locDataArray.removeAll()
let url = "http://someurl/Report/v1/TripReport?UserID=101&Username=ewe2020&DeviceID=2647&FromDate=25/10/2018%2006:00%20AM&lang=ar&ToDate=25/10/2018%2009:00%20PM"
// or if you need the string
print(url)
Alamofire.request(url).responseJSON { (responseData) -> Void in
if((responseData.result.value) != nil) {
let swiftyJsonVar = JSON(responseData.result.value!)
if let data = swiftyJsonVar.stringValue.data(using: .utf8) {
if let json = try? JSON(data: data) {
for item in json["data"].arrayValue {
self.locDataArray.append(LocData(fromJson: item))
}
}
DispatchQueue.main.async{
self.getAddress()
self.table.reloadData()
}
}
}
}
}
用于获取位置的经度和纬度 这是 google api 的:
for loc in self.locDataArray{
let urlComponents = URLComponents(string: "https://maps.googleapis.com/maps/api/geocode/json?latlng="+loc.fromLat!+","+loc.fromLong!+"&key=*************")!
Alamofire.request(urlComponents).responseJSON { (responseData) -> Void in
if((responseData.result.value) != nil) {
let swiftyJsonVar = JSON(responseData.result.value!)
self.locNameFrom.append((swiftyJsonVar.dictionary!["results"]?[0]["formatted_address"].string)!)
} else {
print("empty")
}
}
}
最后,在 cellForRowAt 中
if locNameFrom.count > 0 {
cell?.from.text = locNameFrom[indexPath.row]
}
数据仅在向上或向下滚动后显示。
最佳答案
if((responseData.result.value) != nil) {
let swiftyJsonVar = JSON(responseData.result.value!)
self.locNameFrom.append((swiftyJsonVar.dictionary!["results"]?[0]["formatted_address"].string)!)
self.table.reloadData() // <-- here you should reloadData.
}
关于swift - TableView 在使用两个嵌套的 alamofire 请求滚动之前不会显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53108097/