0

我是 Swift 和 Stackoverflow 的新手,我想知道是否有一种方法可以将数据从我的 plist 导入 detailCalloutAccesoryView,其方式与我在地图注释标注中导入“标题”和“副标题”的方式相同?这样,我可以避免制作自己的自定义地图注释,而是使用 MapKit 的内置功能。

我将我的变量称为标题和副标题,如下所示:

var myData: NSArray?
        if let path = NSBundle.mainBundle().pathForResource("myData", ofType: "plist") {
            myData = NSArray(contentsOfFile: path)
        }
        if let items = myData {
            for item in items {
                let lat = item.valueForKey("Latitude") as! Double
                let long = item.valueForKey("Longitude") as! Double
                let myAnnotation = Mydata(value1: "Value1", value2: "Value2", value3: "Value3", latitude: lat, longitude: long)
                // Define "Value1 to be shown as title in Callout
                myAnnotation = item.valueForKey("Value1") as? String
                // Define "Value2 to be shown as subtitle in Callout
                myAnnotation.subtitle = item.valueForKey("Value2") as? String
                annotations.append(myAnnotation)
            }
        }
        return annotations
    }

到目前为止,我只是使用以下代码在 detailCalloutAccessoryView 的所有注释中显示相同的值来代替字幕:

let detailAccessory = UILabel(frame: CGRectMake(0,0,50,30))
        detailAccessory.text = "Value3" // Obviously, shows constant value for all annotations
        detailAccessory.font = UIFont(name: "Verdana", size: 10)
        pinView?.detailCalloutAccessoryView = detailAccessory

请不要让我的无知惹恼你...

4

1 回答 1

1

您将需要实现 func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? 委托方法。在那里你可以自定义 detailCalloutAccessoryView。

详细解释参考https://developer.apple.com/videos/play/wwdc2015/206/

于 2016-06-09T18:39:09.977 回答