Swift Google 表视图中的位置

Swift Google Places in a tableview

我想做的是创建一个 tableView,其中包含取自 Google 的地方 api 的一堆不同餐厅的名称。 Google 建议获取我能够做到的 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
 }

}

无论我做什么,我都无法填充数组。我检查了 info plist,我需要的一切都在那里,但我认为问题出在 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()
           } 
        }
    }
}