【发布时间】:2023-03-19 17:44:01
【问题描述】:
好的,所以我有一个表格视图,当加载视图时,它会填充 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 不断地 或者只要用户的位置发生变化,我预测这会经常发生。因为它需要显示范围内的项目,所以我不能让用户开车到某个地方,表格仍然显示最后一个位置的项目。
对于应用程序的其他部分,我只是在单击某个按钮时刷新了表格,但是我需要知道如何始终或每当用户的位置发生变化时正确更新此表格。我尝试使用设置为几分之一秒的计时器,但这会导致问题并且似乎不是正确的方法。
我该怎么做?关于总是更新表格,我试过了
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")
}
【问题讨论】:
-
注册以在应用程序处于前台时收到重大位置更改的通知并立即重新加载表格视图,或者在应用程序进入前台时设置标志并重新加载表格视图。
-
进入前台是什么意思?有特定的函数可以调用吗?
-
不,AppDelegate 中有一个委托方法被调用。我只是想指出,在应用未激活时重新加载表格视图是没有意义的。
-
好的.. 但我的意思是当他们使用该应用程序时会不断更新。有没有一种方法可以推荐您跟踪重大的位置变化?只是将以前的值存储在临时或..?
标签: swift uitableview parse-platform reloaddata