把我的TableViewcell与我的距离sorting来自swift3中的响应

即时通讯工作在地图和tableview。 我有4个地点,我有彼此之间的距离,现在我想要当我点击任何单元格,那么该单元格应该来第一个位置,之后与sorting所有其他位置将放置。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let indexPath = tableView.indexPathForSelectedRow let currentCell = tableView.cellForRow(at: indexPath!)! print(currentCell.textLabel!.text as Any) print(indexPath?.row as Any) print(indexPath as Any) if (indexPath?.row) != 0{ tableView.beginUpdates() self.tableView.moveRow(at: IndexPath(row: (indexPath?.row)!, section: 0), to: IndexPath(row: 0, section: 0)) // self.tableView.moveRow(at: IndexPath(row: 0, section: 0), to: IndexPath(row: (indexPath?.row)!, section: 0)) tableView.endUpdates() tableView.reloadData() } //posts.sort { ($0 as AnyObject).distanceInMeters < $1.distanceInMeters } let lat = "\((self.posts[(indexPath?.row)!] as AnyObject).value(forKey: "location_latitude")!)" let lon = "\((self.posts[(indexPath?.row)!] as AnyObject).value(forKey: "location_longitude")!)" let convertToDouble1 = Double((lat as NSString).doubleValue) let convertToDouble2 = Double((lon as NSString).doubleValue) let location:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: convertToDouble1, longitude: convertToDouble2) let dropPin = MKPointAnnotation() dropPin.coordinate = location dropPin.title = "\((self.posts[(indexPath?.row)!] as AnyObject).value(forKey: "name")!)" self.map.addAnnotation(dropPin) self.map.showsUserLocation = true self.map.showAnnotations(annotations, animated: true) self.map.selectAnnotation(dropPin, animated: true) } 

在这里输入图像说明

在这里输入图像说明