通过double对UITableView进行排序

时间:2018-04-08 15:55:41

标签: swift firebase firebase-realtime-database

我正在尝试创建一个UITableView,它根据用户的距离对商店进行排序。虽然我的距离计算器有效,但我不知道如何通过此值对UITableView进行排序,即h。这是我目前的代码:

  @IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var nearbyTableView: UITableView!
var myList: [String] = []
var handle:DatabaseHandle?
var ref:DatabaseReference?
    var locationManager:CLLocationManager!



func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return myList.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
    var (cellName) = myList[indexPath.row]
    cell.textLabel?.text = cellName
    return cell

}

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
    var (cellName) = myList[indexPath.row]

     let viewController = self.storyboard?.instantiateViewController(withIdentifier: "Home2") as! ShopViewController
    viewController.name = cellName
    self.present(viewController, animated: true, completion: nil)
    navigationController?.pushViewController(viewController, animated: true)

    print("row\(indexPath.row)")
    print("name: \(cellName)")
}

override func viewDidLoad() {
    super.viewDidLoad()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)





            determineMyCurrentLocation()
}



func determineMyCurrentLocation() {
    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()

    if CLLocationManager.locationServicesEnabled() {
        locationManager.startUpdatingLocation()
        //locationManager.startUpdatingHeading()
    }
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let messageDB = Database.database().reference().child("shops")

    messageDB.observe(.childAdded, with: { snapshot in

        let snapshotValue = snapshot.value as! NSDictionary
        let text = snapshotValue["name"] as! String
        let lat = snapshotValue["lat"] as! Double
        let long = snapshotValue["long"] as! Double

        print(text)
        print(lat)
        print(long)





    let userLocation:CLLocation = locations[0] as CLLocation

    // Call stopUpdatingLocation() to stop listening for location updates,
    // other wise this function will be called every time when user location changes.

    // manager.stopUpdatingLocation()





    print("user latitude = \(userLocation.coordinate.latitude)")
    print("user longitude = \(userLocation.coordinate.longitude)")





            if lat < userLocation.coordinate.latitude && long < userLocation.coordinate.longitude {
                let adj = userLocation.coordinate.latitude - lat
                let opp = userLocation.coordinate.longitude - long
                let t = (adj * adj) + (opp * opp)
                let h = t.squareRoot()
                print("distance to shop = \(h)")
            }

            if lat > userLocation.coordinate.latitude && long > userLocation.coordinate.longitude {
                let adj = lat - userLocation.coordinate.latitude
                let opp = long - userLocation.coordinate.longitude
                let t = (adj * adj) + (opp * opp)
                let h = t.squareRoot()
                print("distance to shop = \(h)")

            }


            if lat > userLocation.coordinate.latitude && long < userLocation.coordinate.longitude {
                let adj = lat - userLocation.coordinate.latitude
                let opp = userLocation.coordinate.longitude - long
                let t = (adj * adj) + (opp * opp)
                let h = t.squareRoot()
                print("distance to shop = \(h)")


            }

            if lat < userLocation.coordinate.latitude && long > userLocation.coordinate.longitude {
                let adj = userLocation.coordinate.latitude - lat
                let opp = long - userLocation.coordinate.longitude
                let t = (adj * adj) + (opp * opp)
                let h = t.squareRoot()
                print("distance to shop = \(h)")

            }

            if lat - userLocation.coordinate.latitude == 0 && long > userLocation.coordinate.longitude {


                let h = long - userLocation.coordinate.latitude
                print("distance to shop = \(h)")

            }

            if lat - userLocation.coordinate.latitude == 0 && long < userLocation.coordinate.longitude {

                let h = userLocation.coordinate.longitude - long
                print("distance to shop = \(h)")

            }


            if long - userLocation.coordinate.longitude == 0  && lat > userLocation.coordinate.longitude {

                let h = lat - userLocation.coordinate.latitude
                print("distance to shop = \(h)")

            }


            if long - userLocation.coordinate.longitude == 0  && lat < userLocation.coordinate.longitude {

                let h = userLocation.coordinate.latitude - lat
                print("distance to shop = \(h)")

            }

        self.myList.append(text)
        self.myList.sort() { $0.h > $1.h }
        self.nearbyTableView.reloadData()


    })

}


func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
{
    print("Error \(error)")
}

if语句计算商店的距离。但是,我尝试了很多不同的东西,但我不知道如何排序“myList”#39;有这个值。它说'myList没有会员h&#39;。非常感谢任何帮助,非常感谢。

1 个答案:

答案 0 :(得分:0)

你必须:

  1. 根据距离或您所依赖的任何值对数据数组进行排序。
  2. 调用tableview的reloadData函数生效。
  3. 我希望这会有所帮助