Thank you for coming here.
The problem is that all cells coming with only one distanceInMeters, all 100 objects have the same coordinates. How to fix it?
Thank you for your help!
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
let currentLocation = locations[0]
let coords = CLLocation(latitude: currentLocation.coordinate.latitude, longitude: currentLocation.coordinate.longitude)
if (currentLocation.horizontalAccuracy > 0 ) {
locationManager.stopUpdatingLocation()
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! EateriesTableViewCell
let mall = mallToDisplayAt(indexPath: indexPath)
cell.thumbnailImageView.image = UIImage(named: mall.image)
cell.thumbnailImageView.clipsToBounds = true
cell.nameLabel.text = mall.name
cell.locationLabel.text = mall.location
cell.typeLabel.text = mall.time
let coords = CLLocation()
let mallLocate = CLLocation(latitude: malls[0].latitude, longitude: malls[0].longitude)
let distanceInMeters = mallLocate.distance(from: coords)
let distanceInMetersString = String(distanceInMeters)
cell.distanceLabel.text = distanceInMetersString
return cell
}
You have two big problems in your cellForRowAt
method.
coords
is an empty CLLocation
, not the user's current location. You should store the user's current location in a property and use that property instead of the coords
local variable.malls[0]
instead of mall
to create mallLocate
. This is why all 100 objects appear to have the same coordinates.