gpt4 book ai didi

ios - Miles Away 重新加载时未更新

转载 作者:行者123 更新时间:2023-11-30 12:23:58 24 4
gpt4 key购买 nike

我有一个表格 View 单元格,里面有一个 uilabel ,它获取两点之间的距离(用户当前位置和我选择的另一个点)。问题是,当我更改数据库中的点并重新加载表格 View 时,每个单元格中的英里数都会更新。但是,当我更改模拟器中的当前位置并重新加载表格 View 时,几英里之外的位置不会更新。这是我的代码。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath, object: PFObject?) -> PFTableViewCell? {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cells", for: indexPath as IndexPath) as! MyCell
cell.object = object
let currentLocation = CLLocation()
locManager.distanceFilter = 50
if( CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse ||
CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedAlways){
let coordinate₀ = CLLocation(latitude: CLLocationDegrees(-41)!, longitude: CLLocationDegrees(52.3)!)
let coordinate₁ = CLLocation(latitude: currentLocation.coordinate.longitude, longitude: currentLocation.coordinate.latitude)
let distanceInMeters = coordinate₀.distance(from: coordinate₁)
if(distanceInMeters <= 1609)
{
cell.Distance.text = "\(Float(round(distanceInMeters * 3.28084)).cleanValue) ft"
}
else
{
cell.Distance.text = "\(Float(round(distanceInMeters / 1609)).cleanValue) mi"
}
}
}

最佳答案

您需要在您的viewController中定义您的CLLocationManager,然后在didUpdateLocations中您需要重新加载您的tableView

import UIKit
import CoreLocation

class MyViewController: UIViewController, CLLocationManagerDelegate {

@IBOutlet weak var LabelTest: UILabel!
var manager = CLLocationManager()
@IBOutlet weak var tableView: UITableView!

//your default position
var userLoc = CLLocation(latitude: 42.6977,longitude: 23.3219)
{
didSet{
self.tableView.reloadData()
}
}
//**Your class implementation**/

override func viewDidLoad() {
super.viewDidLoad()

//**Your implementation*//

manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userlocation: CLLocation = locations[0] as CLLocation
self.userLoc = userlocation
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath, object: PFObject?) -> PFTableViewCell? {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cells", for: indexPath as IndexPath) as! MyCell
cell.object = object
let currentLocation = self.userLoc

let coordinate₀ = CLLocation(latitude: CLLocationDegrees(-41), longitude: CLLocationDegrees(52.3))
let coordinate₁ = CLLocation(latitude: currentLocation.coordinate.longitude, longitude: currentLocation.coordinate.latitude)
let distanceInMeters = coordinate₀.distance(from: coordinate₁)
if(distanceInMeters <= 1609)
{
cell.Distance.text = "\(Float(round(distanceInMeters * 3.28084)).cleanValue) ft"
}
else
{
cell.Distance.text = "\(Float(round(distanceInMeters / 1609)).cleanValue) mi"
}
}
}

希望这有帮助

关于ios - Miles Away 重新加载时未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44441695/

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