【发布时间】:2018-05-10 17:17:30
【问题描述】:
我如何在选择 tableview 单元格后将 MKAnnotationView 放置在指定的注释上?我是否需要在表视图的 didSelectRowAtIndexPath 中调用函数 mapView(_:didSelect:) ?这是我的 didselectrow 函数。我确实尝试过在 didselectrow 中使用 map.selectAnnotation,但返回 nil。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let pin = filteredDetails[indexPath.row].coordinate
let latDelta: CLLocationDegrees = 0.009
let lonDelta: CLLocationDegrees = 0.009
let span = MKCoordinateSpanMake(latDelta, lonDelta)
let location = CLLocationCoordinate2DMake(pin.latitude, pin.longitude)
let region = MKCoordinateRegionMake(location, span)
mapView.setRegion(region, animated: true)
// mapView.selectAnnotation(customXibView as! MKAnnotation, animated: true)
tableView.isHidden = true
}
这是我的地图 didselect 功能。
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
let meridianAnnotation = view.annotation as! Details
let customXibView = Bundle.main.loadNibNamed("CustomCalloutVIew", owner: self, options: nil)?[0] as! CustomAnnotationClass!
customXibView?.address.text = meridianAnnotation.address
customXibView?.phone.text = meridianAnnotation.phone
let calloutFrame = view.frame
customXibView?.frame = CGRect(x: (calloutFrame.size.width)-178.0, y: (calloutFrame.size.height)-220, width: 315, height: 166)
customXibView?.blurOverlay.effect = UIBlurEffect(style: .light)
customXibView?.layer.masksToBounds = true
let shadowView = UIView(frame: CGRect(x: (calloutFrame.size.width)-178.0, y: (calloutFrame.size.height)-220, width: 315, height: 154))
shadowView.translatesAutoresizingMaskIntoConstraints = false
shadowView.backgroundColor = UIColor.clear
shadowView.layer.borderWidth = 7.0
shadowView.layer.borderColor = UIColor.lightGray.cgColor
shadowView.alpha = 1.0
shadowView.layer.cornerRadius = 10.0
shadowView.layer.shadowOffset = CGSize(width: 4.0, height: 4.0)
shadowView.layer.shadowOpacity = 1.0
shadowView.layer.shadowRadius = 13.0
view.insertSubview(shadowView, at: 0)
view.addSubview(customXibView!)
mapView.setCenter((view.annotation?.coordinate)!, animated: true)
}
【问题讨论】:
-
mapView.selectAnnotation 仅在您传递已添加到 mapview 中的注释对象时有效
-
好吧,我的对象已经全部添加到地图中,当我按下单元格时,它以相关注释为中心,我想要的只是,另外要弹出标注视图,就像我选择了它一样....
标签: ios swift uitableview mapkit mkannotationview