2012-09-24 144 views
0

我使用來自plist的數據在地圖上加載了一堆針腳。這裏是我得到的數據:獲取indexPath for mapView註釋

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?

回答

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); 
+0

你的天才!像你解釋的一樣工作。謝謝! – user984248