Swift混合tableviewcell和map,选择tableview单元格并选择注释

时间:2016-06-26 09:03:03

标签: ios swift mapkit

我将tableview和apple map混合到一个视图中,我希望当我选择tableview单元格时,也可以选择关联注释。

我是这样做的:

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()

       }

但是当我选择单元格时,也会选择不相关的注释。我假设这是因为注释也包含userLocation,如何解决这个问题,使注释的顺序与tableview单元格indexpath.row的顺序相同?并将中心移动到选定的注释? 谢谢你的回复!

1 个答案:

答案 0 :(得分:0)

我找到了一种简单的方法来实现我的需要,只需使用跨度效果就可以了!

这里的代码显示有人想要产生这种效果。

   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()
}