Johnny  Hsieh Johnny Hsieh - 5 months ago 21
iOS Question

Swift mix tableviewcell and map, select the tableview cell and the annotation also be selected

I mix up tableview and apple map into one view, I hope when I select the tableview cell and the relate annotation can also be selected.

Here is how I do it:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){

var selectedCell:UITableViewCell
selectedCell = tableView.cellForRowAtIndexPath(indexPath)!
self.mapView.selectAnnotation(self.mapView.annotations[indexPath.row], animated: true)
mapTableView.reloadData()

}


but when I select the cell the unrelate annotation also be selected.I assume thats because the annotation also contain userLocation, how can I solve this problem to make annotation's order same as tableview cell indexpath.row's order? And also move center to selected annotation?
Thanks for any reply!

Answer

I find a easy way to achieve my needy it's simple just using span effect!

The code is showing here incase someone like to make this kind of effect.

   func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
    //not finished yet
    var selectedCell:UITableViewCell
    selectedCell = tableView.cellForRowAtIndexPath(indexPath)!
    var location = tripspots[indexPath.row].coordinate
    //resolution
    var span = MKCoordinateSpanMake(0.01, 0.01)
    var region = MKCoordinateRegion(center: location,span:span)
    //move to the specific area!
    self.mapView.setRegion(region, animated: true)
    mapTableView.reloadData()
}