以下是要调用的类:
import Foundation
import MapKit
class Requests {
private var _username:String!
private var _key:String!
var distanceFromDriver:Double! = 0.0
private var _latitude:CLLocationDegrees!
private var _longitude:CLLocationDegrees!
private var _location:CLLocation!
private var _driverLocation:CLLocation!
var username:String {
return _username
}
var key:String {
return _key
}
var latitude:CLLocationDegrees {
return _latitude
}
var longitude:CLLocationDegrees {
return _longitude
}
var location: CLLocation {
return self._location
}
init(uid:String, dictionary: Dictionary<String, AnyObject>, driverLocation:CLLocation, tView: UITableView) {
self._key = uid
self._driverLocation = driverLocation
if let lat = dictionary["Latitude"] as? Double {
self._latitude = lat
}
if let long = dictionary["Longitude"] as? Double {
self._longitude = long
}
if let email = dictionary["Email"] as? String {
self._username = email
}
let riderLocation = CLLocation(latitude: self._latitude, longitude: self._longitude)
self._location = riderLocation
let distanceInKM = _driverLocation.distanceFromLocation(riderLocation)
let miles = (distanceInKM / 1000) * 0.62137
self.distanceFromDriver = miles
tView.reloadData()
}
}
以下是需要检索上述内容的类:
import UIKit
import Firebase
import MapKit
import CoreLocation
import FirebaseAuth
class DriverVC: UITableViewController, MKMapViewDelegate, CLLocationManagerDelegate {
var requests = [Requests]()
var locationManager:CLLocationManager!
@IBOutlet var tableview: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.allowsBackgroundLocationUpdates = true
locationManager.startUpdatingLocation()
self.tableView.registerNib(UINib(nibName: "cell", bundle: nil),
forCellReuseIdentifier: "reuseIdentifier")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.requests.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
if self.requests.count >= 1 {
self.requests.sortInPlace({$0.distanceFromDriver < $1.distanceFromDriver})
cell.textLabel!.text = String(format: "%.01f \(self.requests[indexPath.row].username)", self.requests[indexPath.row].distanceFromDriver)
} else {
self.requests = []
self.tableView.reloadData()
}
return cell
}
}
答案 0 :(得分:0)
我没有足够的评论点,买我的后续是:显示的是什么(即用户名/距离没有显示,是否有其他显示?如果你在{{1}中插入print()
语句你注意到被叫了吗?