索引i处的对象仅显示最后一个图像

时间:2013-06-20 02:16:41

标签: objective-c image nsarray

在以下方法中:

- (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;

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

2 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分:0)

btw你的条件是否完全相互排斥。而不是

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

做:

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

如果你真的需要这一切。

它会减少你的碳足迹。

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