2016-08-15 66 views
1

好了,所以我有,該視圖被加載時,它與具有用戶的當前位置的設定範圍內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

回答

0

設置distanceFilter屬性到你的位置的經理。

locationManager.distanceFilter = 50 

現在只會更新用戶的位置,如果它已更改50或更多米。

+0

好的,我會這樣做 - 你能嗎看看我的編輯,看看我所做的工作也會起作用嗎?它似乎並沒有被重裝tableview中,即使它引發的位置 – skyguy

+0

我應該怎麼改變一個顯著的變化? – skyguy

+0

我沒有看到你在編輯中加入了這個...你應該把它放在if語句中。 – penatheboss