我怎样才能不断更新tableview每'位置变化'?

问题描述:

好了,所以我有,该视图被加载时,它与具有用户的当前位置的设定范围内geopoints解析对象填充的的tableview。这个作品在这里:我怎样才能不断更新tableview每'位置变化'?

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不断或只是每当用户的位置变化,这,我预测,会非常频繁发生。因为它需要显示范围内的项目,所以我不能让用户开车到某个地方,并且表格仍显示最后一个地点的项目。

对于应用程序我有其他部位只是有表刷新点击某个按钮时,不过,我需要知道如何正确ALWAYS或每当更新此表中的用户的位置变化。我试图使用计时器设置为几分之一秒,但是这引起了问题,似乎并不是正确的方法。

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

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

但这没有做什么。看着loadObjects()也有,但有错误。达到此目的的最佳方法是什么?

EDITS:

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") 
    } 
+0

注册通知的显著位置的改变,并立即重新加载表视图时,应用程序是在前台或设置一个标志,当应用程序来到前台重新加载表视图。 – vadian

+0

你是什么意思来到前景?有什么功能可以调用吗? – skyguy

+0

不,在AppDelegate中有一个委托方法被调用。我只是想指出,重新加载表视图,而应用程序不活跃是没有意义的。 – vadian

设置distanceFilter属性到你的位置的经理。

locationManager.distanceFilter = 50 

现在只会更新用户的位置,如果它已更改50或更多米。

+0

好的,我会这样做 - 你能吗看看我的编辑,看看我所做的工作也会起作用吗?它似乎并没有被重装tableview中,即使它引发的位置 – skyguy

+0

我应该怎么改变一个显著的变化? – skyguy

+0

我没有看到你在编辑中加入了这个...你应该把它放在if语句中。 – penatheboss