如何将CustomAnnotation类调用到viewforannotation中?从现在看来,它只会在点击注释时崩溃。
class CustomAnnotation: MKPinAnnotationView, MKMapViewDelegate, CLLocationManagerDelegate {
var annotationData : [Apartments] = []
var fishAnnoTitle : String!
var fishAnnoImage : NSData!
let selectedLabel : UILabel = UILabel.init(frame:CGRectMake(0, 0, 300, 38))
let selectedImage : UIImageView = UIImageView.init(frame:CGRectMake(0, 35, 300, 200))
var selectedButton: UIButton = UIButton(type: UIButtonType.Custom) as UIButton!
override func setSelected(selected: Bool, animated: Bool)
{
super.setSelected(false, animated: animated)
if(selected)
{
selectedLabel.textAlignment = .Center
selectedLabel.textColor = UIColor.darkGrayColor()
selectedLabel.font = UIFont.init(name: "Trebuchet MS", size: 20)
selectedLabel.backgroundColor = UIColor.whiteColor()
selectedLabel.layer.borderColor = UIColor.darkGrayColor().CGColor
selectedLabel.layer.borderWidth = 2
selectedLabel.layer.cornerRadius = 5
selectedLabel.layer.masksToBounds = true
selectedImage.layer.cornerRadius = 5
selectedImage.layer.borderWidth = 2
selectedImage.layer.masksToBounds = true
selectedButton.frame = CGRectMake(262, 0, 38, 38)
selectedButton.layer.borderColor = UIColor.darkGrayColor().CGColor
selectedButton.setImage(UIImage(named: "advance.png"), forState: .Normal)
selectedButton.addTarget(self, action: "buttonPressed", forControlEvents: UIControlEvents.TouchUpInside)
let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context: NSManagedObjectContext = appDel.managedObjectContext
let freq = NSFetchRequest(entityName: "Apartments")
let fetchResults = try! context.executeFetchRequest(freq) as! [Apartments]
self.annotationData = fetchResults
let countImages = self.annotationData.count
if countImages != 0 {
for row in 0...countImages-1 {
let dataImage : Apartments = self.annotationData[row]
self.fishAnnoImage = (dataImage.valueForKey("lejlighedBillede") as? NSData)!
self.fishAnnoTitle = "\(dataImage.valueForKey("lejlighedHusleje")!)"
selectedImage.image = UIImage(data: dataImage.lejlighedBillede!)
self.addSubview(selectedImage)
selectedLabel.text = self.fishAnnoTitle!
self.addSubview(selectedLabel)
self.addSubview(selectedButton)
}
}
}
else
{
selectedLabel.removeFromSuperview()
selectedImage.removeFromSuperview()
selectedButton.removeFromSuperview()
}
}
}
现在我想使用自定义类,所以我可以进行自定义注释。
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier("CustomAnnotation") as CustomAnnotation
annotationView = CustomAnnotation(annotation: annotation, reuseIdentifier: "CustomAnnotation")
return annotationView
}
答案 0 :(得分:0)
为什么要尝试将其设为正常注释而不是自定义?这没有任何意义,你需要将它作为你的注释出列
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier("CustomAnnotation") as CustomAnnotation!
和自定义注释需要符合MKAnnotation