gpt4 book ai didi

ios - 来自 Firebase 的数组不会附加

转载 作者:行者123 更新时间:2023-11-28 06:01:02 25 4
gpt4 key购买 nike

我的代码最近可以正常工作,但我更改了很多代码,现在我不明白为什么,但现在我从 firebase 获取的数组正在打印,但没有附加到 UITableView。它以前工作过,但我不知道出了什么问题。此外,当它工作时,它也在复制。这是我当前的代码:

    @IBOutlet weak var nearbyTableView: UITableView!
var myList: [String] = []
var handle:DatabaseHandle?
var ref:DatabaseReference?
var locationManager:CLLocationManager!


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myList.count
}

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

let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
var (cellName) = myList[indexPath.row]
cell.textLabel?.text = cellName
return cell

}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
var (cellName) = myList[indexPath.row]



let viewController = self.storyboard?.instantiateViewController(withIdentifier: "Home2") as! ShopViewController
viewController.name = cellName
self.present(viewController, animated: true, completion: nil)
navigationController?.pushViewController(viewController, animated: true)

print("row\(indexPath.row)")
print("name: \(cellName)")
}


override func viewDidLoad() {
super.viewDidLoad()
determineMyCurrentLocation()
}

func determineMyCurrentLocation() {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()

if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingLocation()
}

}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

let messageDB = Database.database().reference().child("shops")

messageDB.observe(.childAdded, with: { snapshot in

let snapshotValue = snapshot.value as! NSDictionary
let text = snapshotValue["name"] as! String

let userLocation:CLLocation = locations[0] as CLLocation



self.myList.append(text)
})

self.nearbyTableView.reloadData()
}

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
{
print("Error \(error)")
}

非常感谢

最佳答案

尝试改变你的 didUpdateLocations 如下,不是 100% 确定,但它可以帮助你

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

let messageDB = Database.database().reference().child("shops")

messageDB.observeSingleEvent(of: .childAdded, with: { snapshot in

let snapshotValue = snapshot.value as! NSDictionary
let text = snapshotValue["name"] as! String

let userLocation:CLLocation = locations[0] as CLLocation

self.myList.append(text)
self.nearbyTableView.reloadData()

})

}

关于ios - 来自 Firebase 的数组不会附加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49733345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com