【问题标题】:How can I constantly update tableview per 'location changes'?如何根据“位置更改”不断更新表格视图?
【发布时间】: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


【解决方案1】:

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

locationManager.distanceFilter = 50

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

【讨论】:

  • 好的,我会这样做 - 你能看看我的编辑,看看我所做的是否也有效?它似乎没有重新加载 tableview,即使它触发了位置的重大变化
  • 我应该改变什么?
  • 我没有看到你在编辑中添加了这个...你应该把它放在 if 语句中。
  • 好的.. 但我的 tableview 似乎没有重新加载数据。那更多是我的问题。不过谢谢
  • 在您的编辑中,我应该使用 startUpdatingLocation() 还是显着位置更改函数?
猜你喜欢
  • 2017-08-29
  • 2023-03-19
  • 2020-04-21
  • 1970-01-01
  • 2011-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多