简体   繁体   中英

CLLocationManager doesn't work for cells on first page of UITableView

I'm using CLLocationManager to get current location, and I print distances (from the current location I got) to each spots in cells of a table.

class TableViewController: UITableViewController, CLLocationManagerDelegate, MKMapViewDelegate {

var thisLocationManager = CLLocationManager()
var myLocation = CLLocation()

...

override func viewDidLoad() {  

   ...

   if CLLocationManager.locationServicesEnabled() {
        self.thisLocationManager.requestAlwaysAuthorization()
        self.thisLocationManager.requestWhenInUseAuthorization()
        thisLocationManager.delegate = self
        thisLocationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        thisLocationManager.startUpdatingLocation()
    }
}

func locationManager(manager: CLLocationManager, didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation) {
    myLocation = newLocation
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> SpotCell {
    let row = self.list[indexPath.row]

    let cell = tableView.dequeueReusableCellWithIdentifier("SpotCell") as! SpotCell

    if CLLocationManager.locationServicesEnabled() {
        // for checking 
        print("Row \(indexPath.row): lati \(myLocation.coordinate.latitude), long \(myLocation.coordinate.longitude)")

        let spotLocation = CLLocation(latitude: Double(row.latitude)!, longitude: Double(row.longitude)!)
        let distance = round(myLocation.distanceFromLocation(spotLocation) / 10) / 100
        cell.spotInfo.text = "\(row.spotrank), distance \(distance)km"
    } else {
        cell.spotInfo.text = row.spotrank
    }

    return cell
}

This is the code I used for getting current location.
When I set a coordinate on my iOS9 simulator and run the app

for the first cells, the console says:
Row 0: lati 0.0, long 0.0
Row 1: lati 0.0, long 0.0
Row 2: lati 0.0, long 0.0
Row 3: lati 0.0, long 0.0
Row 4: lati 0.0, long 0.0
Row 5: lati 0.0, long 0.0

and then when I scroll down...
Row 6: lati 51.530466, long -0.123833
Row 7: lati 51.530466, long -0.123833
Row 8: lati 51.530466, long -0.123833
these are the coordinate I set. It works fine for the next cells.

When I scroll back
Row 2: lati 51.530466, long -0.123833
Row 1: lati 51.530466, long -0.123833
Row 0: lati 51.530466, long -0.123833

So why this doesn't work for the first page only?

It sounds like you haven't loaded up that necessary data when it's asked for it on the first page, but afterwards it's available. You need to implement some method to detect once you have a location available and then go and (re)set the cells with that data.

The good news is you already have that method! Once you receive a message from func locationManager(manager: CLLocationManager, didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation) , call self.tableView.reloadData() and you'll get data flowing in!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM