2017-04-10 111 views
2

有人可以帮助我了解如何按距离组织我的tableview,这已经拉动并显示了mapItems距用户位置有多远的数据。没有看到任何人在实际的tableview上给出答案。谢谢。用户Swift组织UITableView距离MKMapItems

import UIKit 
import MapKit 

class ListedMapTableViewController: UITableViewController, CLLocationManagerDelegate { 

var mapItems: [MKMapItem]! 
var userLocation = CLLocationManager() 
let distanceFormatter = MKDistanceFormatter() 


override func numberOfSections(in tableView: UITableView) -> Int { 
    return 1 
} 

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

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "resultCell", for: indexPath) as! ListedTableViewCell 

    // Configure the cell... 
    let row = indexPath.row 
    let item = mapItems[row] 
    cell.nameLabel.text = item.name 
    cell.detailLabel.text = item.phoneNumber 
    let distanceInMeters : Double = self.userLocation.location!.distance(from: mapItems[row].placemark.location!) 
    let distanceInMiles : Double = ((distanceInMeters.description as String).doubleValue * 0.00062137) 
    cell.distanceLabel.text = "\(distanceInMiles.string(2)) miles away" 


    return cell 
} 

} 

//get string value of double without casting 
extension String { 
var doubleValue: Double { 
    return (self as NSString).doubleValue 
} 
} 

//formats a double's decimal places 
extension Double { 
func string(_ fractionDigits:Int) -> String { 
    let formatter = NumberFormatter() 
    formatter.minimumFractionDigits = fractionDigits 
    formatter.maximumFractionDigits = fractionDigits 
    return formatter.string(from: NSNumber(value: self))! 
} 
} 

编辑

import UIKit 
import MapKit 

class ListedMapTableViewController: UITableViewController, CLLocationManagerDelegate { 

var mapItems: [MKMapItem]! 
var userLocation = CLLocationManager() 
let distanceFormatter = MKDistanceFormatter() 

func sortedMapItems() -> [MKMapItem]! { 
    return self.mapItems.sorted(by: { (a, b) -> Bool in 
     return self.userLocation.location!.distance(from: a.placemark.location!) > self.userLocation.location!.distance(from: b.placemark.location!) 
    }) 
} 

override func numberOfSections(in tableView: UITableView) -> Int { 
    return 1 
} 

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

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "resultCell", for: indexPath) as! ListedTableViewCell 
    // Configure the cell... 
    let row = indexPath.row 
    let item = sortedMapItems()[row] 
    cell.nameLabel.text = item.name 
    cell.detailLabel.text = item.phoneNumber 
    let distanceInMeters : Double = self.userLocation.location!.distance(from: sortedMapItems()[row].placemark.location!) 
    let distanceInMiles : Double = ((distanceInMeters.description as String).doubleValue * 0.00062137) 
    cell.distanceLabel.text = "\(distanceInMiles.string(2)) miles away" 

    return cell 
} 

} 

//get string value of double without casting 
extension String { 
var doubleValue: Double { 
    return (self as NSString).doubleValue 
} 
} 

//formats a double's decimal places 
extension Double { 
func string(_ fractionDigits:Int) -> String { 
    let formatter = NumberFormatter() 
    formatter.minimumFractionDigits = fractionDigits 
    formatter.maximumFractionDigits = fractionDigits 
    return formatter.string(from: NSNumber(value: self))! 
} 
} 

第二个编辑

import UIKit 
import MapKit 

class ListedMapTableViewController: UITableViewController, CLLocationManagerDelegate { 

var mapItems: [MKMapItem]! 
var userLocation = CLLocationManager() 
let distanceFormatter = MKDistanceFormatter() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    func sortMapItems() { 
     self.mapItems = self.mapItems.sorted(by: { (a, b) -> Bool in 
      return self.userLocation.location!.distance(from: a.placemark.location!) > self.userLocation.location!.distance(from: b.placemark.location!) 
     }) 
    } 
} 

override func numberOfSections(in tableView: UITableView) -> Int { 
    return 1 
} 

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

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "resultCell", for: indexPath) as! ListedTableViewCell 
    // Configure the cell... 
    let row = indexPath.row 
    let item = mapItems[row] 
    cell.nameLabel.text = item.name 
    cell.detailLabel.text = item.phoneNumber 
    let distanceInMeters : Double = self.userLocation.location!.distance(from: mapItems[row].placemark.location!) 
    let distanceInMiles : Double = ((distanceInMeters.description as String).doubleValue * 0.00062137) 
    cell.distanceLabel.text = "\(distanceInMiles.string(2)) miles away" 

    return cell 
} 

} 

//get string value of double without casting 
extension String { 
var doubleValue: Double { 
    return (self as NSString).doubleValue 
} 
} 

//formats a double's decimal places 
extension Double { 
func string(_ fractionDigits:Int) -> String { 
    let formatter = NumberFormatter() 
    formatter.minimumFractionDigits = fractionDigits 
    formatter.maximumFractionDigits = fractionDigits 
    return formatter.string(from: NSNumber(value: self))! 
} 
} 

回答

1

排序他们距离:

func sortedMapItems() -> [MKMapItem] { 
    return self.mapItems.sorted(by: { (a, b) -> Bool in 
     return self.userLocation.location!.distance(from: a.placemark.location!) > 
       self.userLocation.location!.distance(from: b.placemark.location!) 
    }) 
} 

编辑:创建一个函数来你mapitems进行排序,然后把它在viewDidLoad中:

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.sortMapItems() 

} 
func sortMapItems() { 
     self.mapItems = self.mapItems.sorted(by: { (a, b) -> Bool in 
      return self.userLocation.location!.distance(from: a.placemark.location!) > self.userLocation.location!.distance(from: b.placemark.location!) 
     }) 
    } 

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

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "resultCell", for: indexPath) as! ListedTableViewCell 
    // Configure the cell... 
    let row = indexPath.row 
    let item = mapItems[row] 
    cell.nameLabel.text = item.name 
    cell.detailLabel.text = item.phoneNumber 
    let distanceInMeters : Double = self.userLocation.location!.distance(from: mapItems[row].placemark.location!) 
    let distanceInMiles : Double = ((distanceInMeters.description as String).doubleValue * 0.00062137) 
    cell.distanceLabel.text = "\(distanceInMiles.string(2)) miles away" 

    return cell 
} 

在调用这个答案在cellForRowRowAtIndexPath原有功能(sortedMapItems)将过重和不必要的,因为该函数将被反复调用。

更好的方法来重新创建自己的数据结构,并从用户的位置添加的每个项目的距离,这样你就不必在你cellForRow..

+0

再次呼吁distance功能我不知道我在执行这是正确的。我把你的代码粘贴到let distanceFormatter = MKDistanceFormatter()// - 然后改变了'mapItems'到'sortedMapItems()'的位置,当我试图跳到这个屏幕时,应用程序停止。没有错误出现。 – LBIMBA

+0

我刚刚在EDIT下发布了代码。 – LBIMBA

+0

@LBIMBA请参阅编辑。排序应该只调用一次,你可以重新分配排序列表到你的'mapItems'数组中。请使用该阵列在'cellForRow ...'中显示您的内容...' – janusbalatbat