sort my TableViewcell with distance comes from res

2019-08-31 09:38发布

im working on map and tableview. i have 4 locations and i have distance between each other, now i want that when i click on any cell then that cell should comes on 1st position and after with sort all other location will be place.

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)

    }

enter image description here

enter image description here

0条回答
登录 后发表回答