我剛剛發現一個奇怪的問題,我的分類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不正確。它似乎是隨機抽取的。
注:上面的代碼已經有一些 部分去除,所以有可能是拼寫錯誤
感謝任何信息。
爲什麼不發佈一些示例代碼,以便我們可以幫助您。 從策略的角度來看,您應該像使用tableview一樣利用重用隊列。假設一個具有不同內容的通用設計 - 爲什麼你想每次都要創建新的內容?這個想法是爲了避免分配和使用重用隊列的效率。如果你開始使用示例代碼或遵循這條道路上的任何文檔,很可能會出現這種情況。 – Nick 2010-08-30 12:57:48
謝謝尼克。我將在此後發佈一些代碼 – 2010-08-30 14:40:50