我想按距离对TableView进行排序,但是我混淆了:(如果可以,请帮助我,我真的很感激:(
我有一个包含100个对象的数组。
var malls: [Mall] = [ Mall(name: "Европейский", type: "торговый центр", image: "europe.jpg", time: "с 10.00 до 22.00", timeWeek: "с 10.00 до 23.00", city: "Москва", location: "Киевская", adress: "г. Москва, Киевского вокзала площадь, д.2", cinemaName:"formulakino.png", productName: "perekrestok.png", numShop: "309", website: "http://europe-tc.ru", schemeWeb:"http://www.europe-tc.ru/shops/", longitude:37.566, latitude:55.745), Mall(name: "Золотой Вавилон Ростокино", type: "торговый центр", image: "vavilon.jpg", time: "с 10.00 до 22.00", timeWeek: "с 10.00 до 22.00", city: "Москва", location: "Проспект Мира", adress: "г. Москва, Проспект Мира, д. 211", cinemaName: "Люксор", productName: "okay.png", numShop: "280", website: "http://zolotoy-vavilon.ru/rostokino", schemeWeb:"http://www.zolotoy-vavilon.ru/rostokino/map", longitude:37.663, latitude:55.846), ]
等等..对不起,西里尔字母
并覆盖func tableView后,我有这个
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { let currentLocation = locations[0] if (currentLocation.horizontalAccuracy > 0 ) { locationManager.stopUpdatingLocation() let coords = CLLocation(latitude: currentLocation.coordinate.latitude, longitude: currentLocation.coordinate.longitude) **let mallLocate = CLLocation(latitude: mall.latitude, longitude: mall.longitude)** let distanceInMeters = mallLocate.distance(from: coords) print (distanceInMeters) } } 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 return cell }
我在这行有问题
let mallLocate = CLLocation(latitude: mall.latitude, longitude: mall.longitude)
使用未解决的标识符“ mall”
我该如何解决?以及如何按照指示进行排序?非常感谢。
=>在该函数的范围内没有任何名为 “ mall”的 东西locationManager didUpdateLocations。含义:locationManager didUpdateLocations不知道内部发生了什么,tableView cellForRowAt indexPath反之亦然。
locationManager didUpdateLocations
tableView cellForRowAt indexPath
您可以通过对代码应用以下步骤来解决此问题:
将距离计算从 locationManager didUpdateLocations 移至 tableView cellForRowAt indexPath
向您的类添加一个存储当前位置的属性: var currentPosition: CLLocation? = nil
var currentPosition: CLLocation? = nil
将您收到的职位存储locationManager didUpdateLocations到currentPosition属性中
currentPosition
self.tableView.reloadData()收到并分配职位后 添加
self.tableView.reloadData()
mall
cellForRowAt
distanceLabel
nil