Miles Away不会在重新加载时更新

时间:2017-06-08 17:01:25

标签: ios objective-c iphone swift swift3

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

class Server{
    public void listen(){
        ServerSocket listenSocket = new ServerSocket(port);

        while(true){
            Socket clientSocket = listenSocket.accept();
            // start new thread for the connection
            new Connection(clientSocket, this).start();
        }
    }

    public void save(){
        //save stuff
    }
}

1 个答案:

答案 0 :(得分:0)

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

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"
        }
    }
}

我希望这会有所帮助