Swift Google 在表格视图中放置



我想做的是创建一个tableView,其中包含从Google的Place api中获取的一堆不同餐厅的名称。谷歌建议获取place_id,我能够做到,并使用该place_id来获取餐厅的名称,我能够做到。我遇到的问题是,要将餐厅的名称放入 tableView 中,我将需要一个函数外部的数组,因此我将使用该place_id获得的地名附加到函数外部的数组中。这里一切都很好,它的编译没有错误,只是当应用程序加载时,tableView 是空白的,我使用 print 语句检查并指出数组为空。

这是代码:

import UIKit
import Firebase
import GooglePlaces
import GoogleMaps
import Firebase
import Alamofire
import SwiftyJSON
class MainViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var tableView: UITableView!
var placesClient: GMSPlacesClient!

var arrayedName = [String]()

var http = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=41.392788,-73.450949&radius=5000&type=restaurant&key=KEY_HERE"
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
placesClient = GMSPlacesClient.shared()
other()
}

func other() {
Alamofire.request(http).responseJSON { (responseData) -> Void in
if ((responseData.result.value) != nil) {
let swiftyJsonVar = JSON(responseData.result.value!)
let results = swiftyJsonVar["results"].arrayValue
for result in results {
let id = result["place_id"].stringValue
var arrayed = [String]()
arrayed.append(id)
for i in 0..<arrayed.count {
let array = arrayed[i]
self.placesClient.lookUpPlaceID(array, callback: { (place, error) -> Void in
if let error = error {
print("Lookup place id query error: (error.localizedDescription)")
return
}
guard let place = place else {
print("No place details for (arrayed[i])")
return
}
let placeName = place.name
self.arrayedName.append(placeName)
})
}
}
}
}
}

func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayedName.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! LocationTableViewCell
cell.nameLbl.text = "(arrayedName[indexPath.row])"
return cell
}

}

无论我做什么,我都无法填充数组。我检查了信息列表,我需要的一切都在那里,但我认为问题出在 other() 函数上。我尝试过的另一件事是从 other() 函数重新创建代码并将其添加到 cellForRowAt,但它只是崩溃。看起来像这样:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! LocationTableViewCell
Alamofire.request(http).responseJSON { (responseData) -> Void in
if ((responseData.result.value) != nil) {
let swiftyJsonVar = JSON(responseData.result.value!)
let results = swiftyJsonVar["results"].arrayValue
for result in results {
let id = result["place_id"].stringValue
var arrayed = [String]()
arrayed.append(id)
for i in 0..<arrayed.count {
let array = arrayed[i]
self.placesClient.lookUpPlaceID(array, callback: { (place, error) -> Void in
if let error = error {
print("Lookup place id query error: (error.localizedDescription)")
return
}
guard let place = place else {
print("No place details for (arrayed[i])")
return
}
let placeName = place.name
var arrayName = [String]()
arrayName.append(placeName)
cell.nameLbl.text = "(arrayName[indexPath.row])"
})
}
}

}
}
return cell
}

我不知道该怎么做。如果有人可以提供帮助,我很感激。如果还有什么我可以回答的,请问。 谢谢

最初arrayedName为空,因此您将不会在tableView中看到任何项目。但是,将所有地名附加到此数组后,您需要重新加载tableView以查看新数据。您应该保持cellForRowAt与您在没有任何 API 调用的情况下第一次尝试时尝试的相同。我已经更新了other方法,如下所示,所以现在它应该可以工作了

func other() {
Alamofire.request(http).responseJSON { (responseData) -> Void in
if ((responseData.result.value) != nil) {
let swiftyJsonVar = JSON(responseData.result.value!)
let results = swiftyJsonVar["results"].arrayValue
let dispatchGroup = DispatchGroup()
for result in results {
let id = result["place_id"].stringValue
dispatchGroup.enter()
self.placesClient.lookUpPlaceID(id, callback: { (place, error) -> Void in
if let error = error {
print("Lookup place id query error: (error.localizedDescription)")
dispatchGroup.leave()
return
}
guard let place = place else {
print("No place details for (id)")
dispatchGroup.leave()
return
}
self.arrayedName.append(place.name)
dispatchGroup.leave()
})
}
dispatchGroup.notify(queue: DispatchQueue.global()) {
self.tableView.reloadData()
} 
}
}
}

最新更新