简体   繁体   中英

MkMapView annotation selection dilemma?

Ok, so I have a map view that has a bunch of annotations on it. Certain annotations when selected need to display extended info in a small table view which i am doing by resizing the mapview to half screen and animating into view a table in the bottom half. If another annotation is selected that doesn't need the extra info then in the didDeselectAnnotationView: method i hide the table and go back to the full map view, rinse and repeat.. So far so good, everything is working great.

The issue i am having though is that if a user selects another annotation while they currently have an annotation selected then didSelectAnnotationView delegate method gets called BEFORE the didDeselectAnnotationView.

This is obviously a problem because i am using these two methods to decide whether or not i need to display/hide the info table below the mapview, see code below:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
if ([view.annotation isKindOfClass:[MapLocation class]]) 
{        
    if ([self.selectedAnnotation numberOfEvents] == 1) 
    {
        mapTableViewIsVisible = NO;
    }
    else if ([self.selectedAnnotation numberOfEvents] > 1)
    {            
        // launch mini tableview
        mapTableViewIsVisible = YES;
    }        

    [self loadMapTableViewWithEvents:self.selectedAnnotation.events 
                       forAnnotation:self.selectedAnnotation];
}
}

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{

if ([view.annotation isKindOfClass:[MapLocation class]]) 
{    
    mapTableViewIsVisible = NO;
    [self loadMapTableViewWithEvents:nil forAnnotation:(MapLocation*)view.annotation];
}
}

So for example if i select an annotation that needs the maptable and i currently have a regular annotation selected then the mapTable is loaded when the didSelectAnnotationView method above is called, however it is immediately hidden again because the didDeselectAnnotationView is called right after.

So far i havent been able to figure out a way to fix this.

Any ideas??

You could check for the case where no annotations are visible in didDeselectAnnotationView and then clean up your tableview on this case only. As all other cases will be handled by didSelectAnnotation view.

Something like:

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view{
  if([[mapView selectedAnnotations] count]==0){
    mapTableViewIsVisible = NO;
    [self loadMapTableViewWithEvents:nil forAnnotation:(MapLocation*)view.annotation];
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM