2010-08-29 24 views
1

我剛剛發現一個奇怪的問題,我的分類mkannotationview。iphone mkannotationview奇怪的問題 - 可能被重用

當我添加前5個標記時,它們都完美地工作。在mkannotationview子類中,我記錄了一條我看到5次的消息。但是,當我刪除所有標記並重新繪製它們時 - 使用所有相同的方法,我只看到NSLog一次。

這就像地圖重複使用現有的註釋視圖一樣?有沒有辦法迫使它每次都使用新的?

[代碼爲UPDATE]

因此原因,我不能再使用(這可能是也可能不是問題)是我創造一個獨特的標籤標記。上標記的標籤包含在ProductPlot.h

@interface ProductPlot : NSObject <MKAnnotation> { 
    NSString *productID; 
    float latitude; 
    float longitude; 
} 
@property (nonatomic, copy) NSString *productID; 

和ProductPlot.m

@implementation ProductPlot 
@synthesize productID; 

- (CLLocationCoordinate2D)coordinate { 
    CLLocationCoordinate2D coord = {self.latitude, self.longitude}; 
    return coord; 
} 

- (NSString *) productID { 
    return productID; 
} 

到個別標記(認爲它像一個產品ID)

所以...參考那麼我將註釋視圖子分類爲ProductPlotView.h

@interface ProductPlotView : MKAnnotationView { 
ProductPlot *product; 
} 

和ProductPlotView.m中的

@implementation ProductPlotView 

- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier { 
    if(self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) { 

     product = (ProductPlot *)annotation; 
     UILabel *plate2 = [[[UILabel alloc] init] autorelease]; 
     plate2.text = product.productID; 
     plate2.frame = CGRectMake(35, 4, 100, 30); 
     plate2.backgroundColor = [UIColor clearColor]; //]clearColor]; 
     [plate2 setFont: [UIFont fontWithName: @"myFont" size: plate2.font.pointSize]]; 

     [self addSubview:plate2]; 
    } 
    self.frame = CGRectMake(0,0,133,40); 

    return self; 
} 

所以後來在我的代碼,我使用

- (void)plotPoint: (int) y latitude: (double) lat longitude: (double) lng productID: (NSString *) pID { 

    ProductPlot *newAnnotation = [[ProductPlot alloc] init]; 
    newAnnotation.latitude = lat; 
    newAnnotation.longitude = lng; 
    newAnnotation.productID = pID; 
    [mapView addAnnotation:newAnnotation]; 

    [newAnnotation release]; 

} 

我也有代碼來處理註釋積點。

- (MKAnnotationView *)mapView:(MKMapView *)lmapView viewForAnnotation:(id <MKAnnotation>)annotation { 



    ProductPlotView *eventView = (ProductPlotView *)[lmapView 
            dequeueReusableAnnotationViewWithIdentifier:@"eventview"]; 
    if(eventView == nil) { 
     eventView = [[[VehicleViewInfo alloc] initWithAnnotation:annotation 
                reuseIdentifier:@"eventview"] 
        autorelease]; 
    } 

    eventView.annotation = annotation; 
    return eventView; 
} 

因此...上面的代碼將productID放在一個標籤上,它是地圖標記。這似乎在第一個繪圖實例上完美工作,但如果我刪除標記(使用[mapView removeAnnotations:mapView.annotations];),則再次調用此函數,它會繪製點確定,但產品ID不正確。它似乎是隨機抽取的。

注:上面的代碼已經有一些 部分去除,所以有可能是拼寫錯誤

感謝任何信息。

+0

爲什麼不發佈一些示例代碼,以便我們可以幫助您。 從策略的角度來看,您應該像使用tableview一樣利用重用隊列。假設一個具有不同內容的通用設計 - 爲什麼你想每次都要創建新的內容?這個想法是爲了避免分配和使用重用隊列的效率。如果你開始使用示例代碼或遵循這條道路上的任何文檔,很可能會出現這種情況。 – Nick 2010-08-30 12:57:48

+0

謝謝尼克。我將在此後發佈一些代碼 – 2010-08-30 14:40:50

回答

0

要回答我自己的問題,我將MKAnnotationView* annotationView = nil;添加到我的- (MKAnnotationView *)mapView:(MKMapView *)lmapView viewForAnnotation:(id <MKAnnotation>)annotation代碼的頂部。

現在它看起來很完美。我仍然很想看看別人是否認爲這是做這件事的正確方法?

+0

重複使用隊列是最佳實踐。你可以像這樣破解它,但你沒有遵循標準的設計模式。您可以使用一小組註釋,但可以考慮以重用方式進行操作。 – Nick 2010-08-31 02:27:57

+0

是的,我完全同意......我想重用,相信我!一次只能在屏幕上顯示10個標記,但即使如此,我寧願去追求速度。我只需要找到一種方法來刪除標記上的標籤 - 或者只是重置其中的文本。我可能錯過了...謝謝。 – 2010-09-01 09:04:33

+0

基本上每個標記都必須是唯一的...所以如果它被重用 - 我需要然後重寫標籤。我會思考並重新發布解決方案(如果確實找到了解決方案) – 2010-09-01 09:14:18

0

我解決了一個類似的問題檢查註解視圖的子視圖。 如果它沒有子視圖我創建一個新的標籤,並將其添加爲子視圖,如果它有子視圖我採取第一個和編輯label.text屬性。 通過這種方式,子視圖總是爲零或只有一個。