distanceInMeters 数组和排序的问题

Posted

技术标签:

【中文标题】distanceInMeters 数组和排序的问题【英文标题】:distanceInMeters Problems with arrays and sort 【发布时间】:2016-12-14 17:19:23 【问题描述】:

我想按距离对我的 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”

我该如何解决?以及如何按方向排序?非常感谢。

【问题讨论】:

【参考方案1】:

使用未解析的标识符“mall”

=> 在函数 locationManager didUpdateLocations 的范围内没有任何名为 "mall" 的内容。含义:locationManager didUpdateLocations 不知道tableView cellForRowAt indexPath 内部发生了什么,反之亦然。


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

    locationManager didUpdateLocations移动距离计算 到tableView cellForRowAt indexPath

    向您的类添加一个存储当前位置的属性:var currentPosition: CLLocation? = nil

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

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

【讨论】:

【参考方案2】:

您必须选择数组的索引:

let mallLocate = CLLocation(latitude: malls[0].latitude, longitude: malls[0].longitude)

【讨论】:

哇!谢谢,怎么做 cell.distanceLabel = distanceInMeters 关于cell.distanceLabel = distanceInMeters我不是你在说什么 请问如何覆盖 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell cell.distanceLabel = distanceInMeters distanceInMeters 是未解析的标识符( @FedericoMalagoni 不要要求人们对您的答案进行投票。如果他们觉得有用,他们会投票,OP 会接受。 OP 也无法投票支持您的问题,因为他们只有 1 个代表。【参考方案3】:

听起来您想根据与用户当前位置的距离对您的购物中心数组进行排序。

我建议您将 var distanceToUser 添加到 Mall 对象。 (使其成为可选的 Double)

locationManager.didUpdateLocations()中,循环遍历malls数组并使用CLLocationdistance(from:)函数计算从新用户位置到每个mall的距离,并将该值存储在每个mall的distanceToUser属性中。

填充完商场数组的distanceToUser 属性后,您可以按该属性对数组进行排序。最后,调用表格视图的reloadData() 方法。编写您的 tableView(cellForRowAt:) 方法以按现有数组顺序显示您的单元格。

在 mall 对象中包含距离属性的原因是性能。计算 2 点之间距离的位置管理器功能实际上有点耗时(它必须进行 3D 触发才能在地球表面上获得准确的计算。)您要避免一遍又一遍地计算距离值.相反,通过批量计算到新更新位置的距离,排序可以将距离值视为已知常量,当位置发生变化时,每个 Mall 对象都会计算一次。

如果您有很多商场对象,您可能需要在后台线程中进行距离计算,然后在计算完成后调用主线程重新加载您的表格视图,但您可以编写计算代码以在其上运行如果在位置更新时发现 UI 滞后,则返回主线程并对其进行重构。

您还需要将位置管理器配置为仅在当前位置发生相当大的变化时更新它。要么,要么输入将新位置与旧位置进行比较的代码,并且仅在距离变化到足以影响 Malls 数组的排序顺序时才对 malls 数组进行重新排序。 (在最高灵敏度读数下,位置管理器会为您提供几乎恒定的微小位置变化流,这意味着您需要不断地重新计算距离并重新排序 Malls 数组。)

【讨论】:

以上是关于distanceInMeters 数组和排序的问题的主要内容,如果未能解决你的问题,请参考以下文章

大小为 500000 的部分排序数组的快速排序分段错误

理解积分除法

[程序员代码面试指南]数组和矩阵问题-数组排序后相邻数的最大差值(桶排序思想)

根据另一个数组排序顺序对多个数组进行排序

排序数组中的“==”不比未排序数组快吗? [重复]

《程序员代码面试指南》第八章 数组和矩阵问题 自然数数组的排序