gpt4 book ai didi

objective-c - 获取 mapView 标注的 indexPath

转载 作者:行者123 更新时间:2023-11-28 20:30:02 25 4
gpt4 key购买 nike

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

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?有没有办法找到填充注释标题的字典的索引路径?

最佳答案

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

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

@property (nonatomic, retain) NSMutableDictionary *sourceDictionary;

创建注释时(在现有循环中),设置此属性以及 title 等:

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

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

MapAnnotation *mapAnn = (MapAnnotation *)view.annotation;
NSLog(@"mapAnn.sourceDictionary = %@", mapAnn.sourceDictionary);

关于objective-c - 获取 mapView 标注的 indexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12558277/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com