MapView里面的自定义单元委托问题

时间:2015-11-26 16:19:28

标签: ios swift uitableview mkmapview

我创建了一个包含mapView的单元格的tableview。我正在添加注释并尝试在它们之间建立路径。我使用来自另一个视图控制器的相同代码做了同样的事情,该控制器工作正常,但在具有地图的tableview自定义单元格中无法生成相同的代码。它没有进入viewForAnnotationrendererForOverlay。我已将mapDelegate启用到视图控制器。那么请问我的问题在哪里?

let cell = tableView.dequeueReusableCellWithIdentifier("cell8") as! MapViewTableViewCell
cell.mapView.delegate = self //Tested didn't work
    cell.mapView.setRegion(cell.mapView.regionThatFits(MKCoordinateRegionMakeWithDistance(sourceLocation.placemark.coordinate, 1800, 1800)), animated: true)

    cell.calculateRoute(cell.endLocations, secondArray: cell.waypointLocations)

        for item in cell.endLocations {

           let annotation = MKPointAnnotation()
        annotation.coordinate = item.placemark.coordinate
            if cell.endLocations.indexOf(item) == 0 {
                annotation.title = "start"
            }
            else {
                annotation.title = "end"
            }
            cell.mapView.addAnnotation(annotation)
        }

        return cell

我已经制作并扩展了地图

extension CarViewController : MKMapViewDelegate{

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

    if (annotation.title! == "start") {

        let pinAnnotation = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "identifier")
        pinAnnotation.pinColor = .Green
        return pinAnnotation
    }
    else if (annotation.title! == "end") {

        let pinAnnotation = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "identifier")
        pinAnnotation.pinColor = .Purple
        return pinAnnotation
    }
    return nil
}

func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer {

    if overlay is MKPolyline {
        let polylineRenderer = MKPolylineRenderer(overlay: overlay)
        polylineRenderer.strokeColor = UIColor.greenColor()
        polylineRenderer.alpha = 0.80
        polylineRenderer.lineWidth = 5
        return polylineRenderer
    }

        return MKPolylineRenderer()
    }
}

0 个答案:

没有答案