我想按距离对 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”
我该如何解决这个问题?以及如何按方向排序?非常感谢。
最佳答案
Use of unresolved identifier 'mall'
=> 在 locationManager didUpdateLocations
函数范围内没有任何名为 "mall" 的内容。含义:locationManager didUpdateLocations
不知道 tableView cellForRowAt indexPath
内部发生了什么,反之亦然。
您可以通过对代码应用以下步骤来解决此问题:
- 将距离计算移至
locationManager didUpdateLocations
到
tableView cellForRowAt indexPath
向您的类添加一个存储当前位置的属性:
var currentPosition:CLLocation? = 无
将您在
locationManager didUpdateLocations
中收到的位置存储到currentPosition
属性- 在收到并分配位置后添加
self.tableView.reloadData()
- 使用
mall
和cellForRowAt
中的currentPosition
计算距离并更新distanceLabel
- 请记住,
currentPosition
可以为nil
并且无法计算距离 => 将标签设为空或添加“未知距离”之类的内容
关于ios - distanceInMeters 数组和排序的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41148504/