0

我正在使用 plist 中的数据在地图上加载一堆图钉。这是我获取数据的方式:

for (int i=0; i<self.dataArray.count; i++){

    NSDictionary *dataDictionary = [self.dataArray objectAtIndex:i];
    NSArray *array = [dataDictionary objectForKey:@"Locations"];

    for (int i=0; i<array.count; i++){

        NSMutableDictionary *dictionary = [array objectAtIndex:i];

        double latitude = [[dictionary objectForKey:@"Latitude"] doubleValue];
        double longitude = [[dictionary objectForKey:@"Longitude"] doubleValue];

        CLLocationCoordinate2D coord = {.latitude =
            latitude, .longitude =  longitude};
        MKCoordinateRegion region = {coord};

        MapAnnotation *annotation = [[MapAnnotation alloc] init];
        annotation.title = [dictionary objectForKey:@"Name"];

        NSString *cityState = [dictionary objectForKey:@"City"];
        cityState = [cityState stringByAppendingString:@", "];
        NSString *state = [dictionary objectForKey:@"State"];
        cityState = [cityState stringByAppendingString:state];
        annotation.subtitle = cityState;
        annotation.coordinate = region.center;
        [mapView addAnnotation:annotation];
    }
}

现在,对于每个注释,我都添加了一个 detailDisclosureButton。我想从 plist 中显示该特定位置的详细信息。问题是我需要 indexPath.section 和 indexPath.row。

如何获取 pin 的 indexPath?有没有办法找到填充注释标题的字典的 indexPath ?

4

1 回答 1

3

dictionary我建议在注释对象中存储对自身的引用,而不是跟踪“部分”和“行” 。

MapAnnotation类中,添加一个属性来保存对源字典的引用:

@property (nonatomic, retain) NSMutableDictionary *sourceDictionary;

创建注释时(在现有循环中),将此属性与title等一起设置:

annotation.sourceDictionary = dictionary;
annotation.title = [dictionary objectForKey:@"Name"];

然后在详细按钮处理程序方法中(假设您使用的是calloutAccessoryControlTapped委托方法),您将注释对象转换为您的类,您将能够访问注释来自的原始字典:

MapAnnotation *mapAnn = (MapAnnotation *)view.annotation;
NSLog(@"mapAnn.sourceDictionary = %@", mapAnn.sourceDictionary);
于 2012-09-24T03:26:14.970 回答