Parse objects as AnnonationPoints

此生再无相见时 提交于 2019-12-13 05:16:05

问题


I'm trying to show a set of parse objects in a MKMapView as Annonations. This seem to work, the problem is that the objects images are swapped, which is kind of random? i've started creating a subclass of the MKPointAnnotation in order to save the image. How come the images are swapped around?

Custom MKPointAnnonation class

class AnnonationClass: MKPointAnnotation {

var itemId: NSString?
var itemImage: PFFile?
var itemdescription: NSString?

}

getting the parse objects and adding them using the setGeoPoint method

override func objectsDidLoad(error: NSError!) {
    super.objectsDidLoad(error)
    if error == nil {

        self.pointMapView?.removeAnnotations(pointMapView?.annotations)

        for object in objects {

            var theObject = object as PFObject

            let dateFormatter = NSDateFormatter()
            dateFormatter.dateFormat = "yyyy-MM-dd" // superset of OP's format
            let str = dateFormatter.stringFromDate(object.createdAt)


            self.setGeoPoint(theObject.objectForKey("location") as PFGeoPoint, titleString: theObject.objectForKey("title") as NSString, imageFile: theObject.objectForKey("image") as PFFile, theId: theObject.objectId as NSString, descString: theObject.objectForKey("description") as NSString, dateString: str)

        }



    }
}

Setting the coordinates, title and saving the imageFile in the itemImage

func setGeoPoint(geoPoint:PFGeoPoint, titleString:NSString, imageFile:PFFile, theId:NSString, descString:NSString, dateString:NSString) {

    var coordinate = CLLocationCoordinate2DMake(geoPoint.latitude, geoPoint.longitude) as CLLocationCoordinate2D
    var pinView:AnnonationClass = AnnonationClass()


            pinView.itemImage = imageFile
            pinView.setCoordinate(coordinate)
            pinView.title = titleString
            pinView.subtitle = dateString

            self.pointMapView!.addAnnotation(pinView)








}

creating the image and setting it

func mapView(mapView: MKMapView!, viewForAnnotation annotation: AnnonationClass!) -> MKAnnotationView! {



    let reuseId = "pin"

    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
    if pinView == nil {
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        pinView!.canShowCallout = true
        pinView!.animatesDrop = true
        pinView!.pinColor = .Purple



        var leftImage: PFImageView = PFImageView(frame: CGRectMake(0, 0, 44, 44))
        leftImage.file = annotation.itemImage
        leftImage.loadInBackground()
        pinView!.leftCalloutAccessoryView = leftImage

    }
    else {
        pinView!.annotation = annotation
    }

    return pinView
}

回答1:


In your viewForAnnotation, when pinView != nil, i.e. when reusing a view, it could well be that the image is set from the previous time that view was used. I think it is analogous to setting check marks on table view cells instead of using the data source to properly set properties for the cell contents.

Try setting the image view whether or not the pinView is reused.

func mapView(mapView: MKMapView!, viewForAnnotation annotation: AnnonationClass!) -> MKAnnotationView! {



let reuseId = "pin"

var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
if pinView == nil {
    pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
    pinView!.canShowCallout = true
    pinView!.animatesDrop = true
    pinView!.pinColor = .Purple

}
else {
    pinView!.annotation = annotation
}

var leftImage: PFImageView = PFImageView(frame: CGRectMake(0, 0, 44, 44))
leftImage.file = annotation.itemImage
leftImage.loadInBackground()
pinView!.leftCalloutAccessoryView = leftImage

return pinView

}



来源:https://stackoverflow.com/questions/26207086/parse-objects-as-annonationpoints

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!