2011-05-26 61 views
5

是否有一種方法(或任何方式),就可以得到該方法中註釋的「索引路徑」:MapView註解indexPath?

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation

我想要做的就是根據註釋得到一個數組裏面的東西。我認爲我想要複製的一種方法是- tableView:cellForRowAtIndexPath:並使用「indexPath」。

搜索了很多,但沒有找到任何結果。或者,也許我只是在尋找不好。任何參考將不勝感激。

+0

如果你只是想要一個註釋數組,不能使用'mapView.annotations'來給出一個註釋數組。 – visakh7 2011-05-26 10:01:34

+0

我需要獲取註釋之外的數據(來自不同的數據集),所以只是註釋不起作用。 – Angelo 2011-05-27 03:15:28

回答

-1

要回答我的問題(這本是如何爲我的作品):

我創造了自定義註解實例和傳輸的數據有,那裏面- mapView:viewForAnnotation:(我使用的自定義pinView)我進口創造了一個我的CustomAnnotation實例並從那裏獲取數據。

[customAnnotation setSomething: data];
- mapView:viewForAnnotation: 
{ 
    pinView = [[[CustomPinView alloc] initWithAnnotation: annotation reuseIdentifier: PinIdentifier] autorelease]; 
}
- initWithAnnotation:reuseIdentifier: 
{ 
  CustomAnnotation *customAnnotation = (CustomAnnotation *)annotation; 

    [customAnnotation something]; 
}

矯枉過正我,但它能夠完成任務。

4
NSUInteger index = [mapView.annotations indexOfObject:annotation]; 

由於底層數組是可變的,請注意index可以更改。這似乎是最接近的indexPath像你可以得到的信息。

+0

是的,但如前所述,索引正在改變,它不會工作(或大多不會工作)。有時候,它會從不同的索引獲取信息。另一件事是,如果用戶位置是打開的,你應該將註釋轉移到一個NSMutableArray並且首先刪除它(索引:0)。 – Angelo 2011-05-27 02:55:49