ios - distanceInMeters 数组和排序的问题

标签 ios arrays swift uitableview

我想按距离对 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 内部发生了什么,反之亦然。

<小时/>

您可以通过对代码应用以下步骤来解决此问题:

  1. 将距离计算移至
    locationManager didUpdateLocations

    tableView cellForRowAt indexPath
  2. 向您的类添加一个存储当前位置的属性:
    var currentPosition:CLLocation? = 无

  3. 将您在 locationManager didUpdateLocations 中收到的位置存储到 currentPosition 属性

  4. 在收到并分配位置后添加 self.tableView.reloadData()
  5. 使用 mallcellForRowAt 中的 currentPosition 计算距离并更新 distanceLabel
  6. 请记住,currentPosition 可以为 nil 并且无法计算距离 => 将标签设为空或添加“未知距离”之类的内容

关于ios - distanceInMeters 数组和排序的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41148504/

相关文章:

c - 使用动态数组的段错误

javascript - 为什么位置移动后我得到一个空数组?

java - 迷失与弦

ios - 如何使用面向协议(protocol)的编程来改进我的 Swift 代码?

ios - 将内容从 Swift 代码注入(inject)到 WKWebView

objective-c - Objective-C : How to make Navigation bar scroll with tableview

ios - 如何检查文件是否存在于用户定义的路径?

ios - 如何使用 Text Kit 构建像 Medium iOS 应用程序这样的富 TextView ?

具有不同单元类实例的 IOS UICollectionView 单元格

ios - 为什么 Obj-C 实例有 1 个保留计数刚创建?