2
誰かが、mapItemsがユーザーの場所からどれだけ離れているかに関するデータを既に引っ張って表示している距離別にtableviewを整理する方法を理解できますか?誰も実際のテーブルビューで答えを見ていない。ありがとうございました。SwiftはユーザーごとにUITableViewを整理します。
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))!
}
}
EDIT
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))!
}
}
SECOND EDIT
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))!
}
}
に再び
distance
関数を呼び出す必要はありませんように、ユーザの位置から各項目の距離を追加これは正しく。 let distanceFormatter = MKDistanceFormatter()//の下にコードを貼り付けてから、 'mapItems'を 'sortedMapItems()'に変更して、この画面に移動しようとするとアプリケーションが停止します。エラーは表示されません。 – LBIMBAEDITの下にコードを掲載しました。 – LBIMBA
@LBIMBA編集を参照してください。並べ替えは一度だけ呼び出す必要があります。並べ替えられたリストを 'mapItems'配列に再度割り当てることができます。この配列を使って 'cellForRow ... 'にコンテンツを表示してください。 – janusbalatbat