2012-05-21 127 views
0

遇到MKAnnotation問題。有一個自定義的pinview,併爲第一次加載工作正常。去除引腳,然後重新加載相同的引腳,它們改變顏色。我添加來自兩個不同的數據庫的引腳,工作得很好。一旦刪除,然後分別添加每個陣列第二個陣列採取第一個陣列自定義引腳,而不是一個分配。MKAnnotations無法正確重新加載

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

MKAnnotationView *pinView = nil; 
if(annotation != mapView.userLocation) 
{ 
    static NSString *defaultPinID = @"pin"; 
    pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID]; 
    if (pinView == nil) 
     pinView = [[MKAnnotationView alloc] 
        initWithAnnotation:annotation reuseIdentifier:defaultPinID]; 

    UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    [rightButton setTitle:annotation.title forState:UIControlStateNormal]; 
    [rightButton addTarget:self 
        action:@selector(showDetails:) 
      forControlEvents:UIControlEventTouchUpInside]; 
    pinView.rightCalloutAccessoryView = rightButton; 

    UIImageView *profileIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"stores.png"]]; 
    pinView.leftCalloutAccessoryView = profileIconView; 

    NSString *badgestringss = @"8 reviews"; 
    customBadge1 = [CustomBadge customBadgeWithString:badgestringss 
             withStringColor:[UIColor whiteColor] 
             withInsetColor:RGB(255, 51, 0) 
             withBadgeFrame:YES 
            withBadgeFrameColor:[UIColor whiteColor] 
              withScale:1.0 
              withShining:YES]; 
    [customBadge1 setFrame:CGRectMake(100, 1, 85, 15)]; 
    [pinView.leftCalloutAccessoryView addSubview:customBadge1]; 



    if(setStoreOrShops==NO){ 
    pinView.image = [UIImage imageNamed:@"stores.png"]; //as suggested by Squatch 
    } 
    else if (setStoreOrShops==YES){ 
     pinView.image = [UIImage imageNamed:@"shops.png"]; 
    } 


    else { 
    [mapView.userLocation setTitle:@"Current Location"]; 
} 
    } 
return pinView; 
} 

已經遍地搜索,但似乎無法得到一個例子工作或一個想法,這是分解。謝謝你的幫助。

+0

作爲我文章中,我想通了。 MyAnnotation * myAnnot =(MyAnnotation *)註釋;如果(myAnnot.mappin == @「42」) pinView.image = [UIImage imageNamed:@「stores.png」]; else pinView.image = [UIImage imageNamed:@「shops.png」]; –

+0

來自這裏http://stackoverflow.com/questions/7288757/how-do-i-load-different-custom-pins-or-identifiers-based-off-of-their-property-v –

回答

1

得到它儘快固定這樣

MyAnnotation *myAnnot = (MyAnnotation *)annotation; 

if (myAnnot.mappin == @"42") 
    customPinView.image = [UIImage imageNamed:@"shops.png"]; 
else 
    customPinView.image = [UIImage imageNamed:@"stores.png"]; 
當然
1

我不確定你的意思是通過改變顏色,因爲我不知道你的CustomBadge代碼是什麼或者你的圖片是什麼樣的。但是,問題可能是您在視圖的初始設置中應用了條件邏輯。後來,當dequeReusableAnnotationViewWithIdentifier:實際上返回一些東西時,它已被配置爲其他東西。

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

    MKAnnotationView *pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"default"]; 
    if (pinView == nil) { 
     // CONFIGURE ONCE 
     // things that are set up once and never change 
    } 

    // CONFIGURE EVERY VIEW 
    // view configurations that change every pin 
} 

您需要將代碼設置爲「配置一次」部分,並將其放入「爲每個視圖配置」部分。否則,每次您打電話給出可重複使用的視圖時,您都可以獲得配置不正確的內容。