sort my TableViewcell with distance comes from response in swift3

荒凉一梦 提交于 2020-01-16 18:34:28

问题


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)

    }

来源:https://stackoverflow.com/questions/44327312/sort-my-tableviewcell-with-distance-comes-from-response-in-swift3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!