0

在以下方法中:

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

我目前有以下 22 个:

if ([[annotation title] isEqualToString:@"Arcadian Winery"]) {
    [profileIconView setImage:[UIImage imageNamed:@"arcadian.png"]];
}
if ([[annotation title] isEqualToString: @"Babcock Winery and vineyards"]) {
    [profileIconView setImage:[UIImage imageNamed:@"babcock.png"]];
}
if ([[annotation title] isEqualToString:@"Bonny Doon Vineyard"]) {
    [profileIconView setImage:[UIImage imageNamed:@"bonnyDoon.png"]];
}

我想用以下数组替换它,但它只返回最后一个对象。

 NSArray *wineryTitle = [NSArray arrayWithObjects:
                       @"Arcadian Winery",
                       @"Babcock Winery and vineyards",
                       @"Bonny Doon Vineyard",
                       @"Castoro Cellars",
                       @"Dry Creek Vineyard",
                       @"Gary Farrell Wines, Inc.",
                       @"Equinox Methode Champenoise",
                       @"Figge Cellars",
                       @"Frog's Leap Winery",
                       @"Heitz Wine Cellars",
                       @"Kathryn Kennedy",
                       @"Merryvale Vineyards",
                       @"Manzoni Estate Vineyard",
                       @"Marilyn Remark Winery",
                       @"Bernardus Vineyards & Winery",
                       @"Nickel & Nickel",
                       @"Opolo Vineyards, Inc.",
                       @"Riverbench Vineyard and Winery",
                       @"Steven Kent Winery",
                       @"Storrs Winery",
                       @"Talley Vineyards",
                       @"Thomas Fogarty Winery",
                       nil];

NSArray *wineryImage = [NSArray arrayWithObjects:
                        @"arcadian.png",
                        @"babcock.png",
                        @"bonnyDoon.png",
                        @"castoro.png",
                        @"dryCreek.png",
                        @"garyFarrell",
                        @"EquinoxMethodeChampenoise.png",
                        @"figge.png",
                        @"frogsLeap.png",
                        @"heitz.png",
                        @"kathrynKennedy.png",
                        @"merryvale.png",
                        @"manzoni.png",
                        @"mailynRemark.png",
                        @"bernardus.png",
                        @"nickelAndNickel.png",
                        @"opolo.png",
                        @"riverbench.png",
                        @"stevenKent.png",
                        @"storrs.png",
                        @"talley.png",
                        @"thomasFogarty.png",
                        nil];

for (int i = 0; i < 22; i++) {

    UIImageView *profileIconView = [[UIImageView alloc] init];

    if ([[wineryTitle objectAtIndex:i] isEqualToString:[wineryTitle objectAtIndex:i]]) {
        [profileIconView setImage:[UIImage imageNamed:[wineryImage objectAtIndex:i]]];    
    }

    profileIconView.frame = CGRectMake(0, 0, 40, 33);
    pinView.leftCalloutAccessoryView = profileIconView;
    [profileIconView release];

}

return pinView;

该数组正确地遍历标题字符串,但始终仅将最后一个图像返回到注释。

4

2 回答 2

1

这保证始终返回最后一项,因为您的if语句中的条件始终为真:

if ([[wineryTitle objectAtIndex:i] isEqualToString:[wineryTitle objectAtIndex:i]]) {
    [profileIconView setImage:[UIImage imageNamed:[wineryImage objectAtIndex:i]]];    
}

您正在比较[wineryTitle objectAtIndex:i][wineryTitle objectAtIndex:i]这始终是正确的。

您的意思是要比较wineryTitle注释的某些属性吗?例如,如果你想使用title注解:

if ([[wineryTitle objectAtIndex:i] isEqualToString:[annotation title]]) {
    // do something
}

或者,如果您想利用新编译器的语法:

if ([wineryTitle[i] isEqualToString:[annotation title]]) {
    // do something   
}

因此产生(在块中添加更多逻辑if)类似于:

for (int i = 0; i < 22; i++) {
    if ([wineryTitle[i] isEqualToString:[annotation title]]) {
        UIImageView *profileIconView = [[UIImageView alloc] init];
        profileIconView.frame = CGRectMake(0, 0, 40, 33);
        profileIconView.image = [UIImage imageNamed:wineryImage[i]];    
        pinView.leftCalloutAccessoryView = profileIconView;
        [profileIconView release];
        break;
    }
}
于 2013-06-20T02:32:45.010 回答
0

顺便说一句,您的 if 条件都是互斥的。代替

if (...) {
    // ...
}
if (...) {
    // ...
}
if (...) {
    // ...
}

做:

if (...)  {
    // ...
} else if (...) {
    // ...
} else if (...) {
    // ...
}

如果你真的需要这一切。

它会减少你的碳足迹。

此外,NSArray具有允许您返回您感兴趣的对象的索引的检查方法。例如.

于 2013-06-20T04:27:07.470 回答