gpt4 book ai didi

swift - 如何根据 'location changes' 不断更新 tableview?

转载 作者:行者123 更新时间:2023-11-28 08:30:31 24 4
gpt4 key购买 nike

好的,所以我有一个 TableView ,当 View 被加载时,它填充了 Parse 对象,这些对象具有用户当前位置的一组范围内的地理点。这在这里有效:

let query: PFQuery = PFQuery(className: "Events")
query.whereKey("location", nearGeoPoint: myGeoPoint, withinMiles: range)

query.findObjectsInBackgroundWithBlock { (objects: [PFObject]?, error) in
if error == nil {
print(objects)

for object in objects! {
self.names.append(object["Name"] as! String)
self.descriptions.append(object["description"] as! String)
}

self.tableView.reloadData()

} else {
print(error)
}
}
}

这个问题是我需要reloadData constantly 或者只是每当用户的位置发生变化时,我预测这会经常发生。因为它需要显示范围内的项目,所以我不能让用户开车到某个地方,而表格仍然显示最后一个位置的项目。

对于应用程序的其他部分,我只是在单击某个按钮时刷新表格,但是我需要知道如何始终或在用户位置发生变化时正确更新此表格。我曾尝试使用设置为几分之一秒的计时器,但这导致了问题并且似乎不是正确的方法。

我该怎么做?关于总是更新表格,我试过

  override func viewDidAppear(animated: Bool) {
self.tableView.reloadData()
}

但是那没有任何作用。也查看了 loadObjects(),但有错误。实现这一目标的最佳方法是什么?

编辑:

if (CLLocationManager.locationServicesEnabled())
{
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
//locationManager.startUpdatingLocation() //so not always updating
locationManager.startMonitoringSignificantLocationChanges()
}


func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// locations contains an array of recent locations, but this app only cares about the most recent
// which is also "manager.location"
myLoc = PFGeoPoint(location: manager.location)
print("significant change - \(myLoc)")
tableView.reloadData()
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError!) {
print("failed")
}

最佳答案

distanceFilter 属性设置为您的位置管理器。

locationManager.distanceFilter = 50

现在它只会在用户位置改变 50 米或更多米时更新用户位置。

关于swift - 如何根据 'location changes' 不断更新 tableview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38958768/

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