1

我正在开发我的第一个 iOS 应用程序,但我被卡住了,我搜索了很多,但找不到任何解决方案。

我有一个 UIPickerView,它有两个组件(Picker1 有 5 个数组对象,Picker2 有 2 个)和一个 UIImageView,我需要根据两个选择器上选择的数据设置不同的图像。

前任:

    Picker1 - A and Picker2 - A | Image - AA
    Picker1 - B and Picker2 - A | Image - BA
    Picker1 - A and Picker2 - B | Image - AB
    Picker1 - B and Picker2 - B | Image - BB
    ...

这是我的代码:

#define MOVEIS 0
#define CORES 1

- (void)viewDidLoad
{
    [super viewDidLoad];

    ContainerPickerView.frame = CGRectMake(0, 390, 320, 207);
    ContainerPickerView.backgroundColor = [UIColor whiteColor];

    CatalogoPicker.showsSelectionIndicator = TRUE;

    self.CoresMoveis=[[NSArray alloc] initWithObjects:@"Preto",@"Vermelho",@"Castanho",@"Cinzento",@"Branco", nil];

    self.CoresChao=[[NSArray alloc] initWithObjects:@"Escuro", @"Claro", nil];

}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == MOVEIS) {
        return [CoresMoveis count];
    }
    if (component == CORES) {
        return [CoresChao count];
    }
    return 0;
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{

    if (component == MOVEIS) {
        return [CoresMoveis objectAtIndex:row];
    }
    if (component == CORES) {
        return [CoresChao objectAtIndex:row];
    }
    return 0;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{

    if ([[CoresMoveis objectAtIndex:[pickerView selectedRowInComponent:0]] isEqual: @"Preto"] || [[CoresChao objectAtIndex:[pickerView selectedRowInComponent:1]] isEqual: @"Escuro"]) {

        _ImageView_Catalogo.image = [UIImage imageNamed:@"movel_preto_chao_escuro.jpg"];

    }
    else if ([[CoresMoveis objectAtIndex:[pickerView selectedRowInComponent:0]] isEqual: @"Vermelho"] || [[CoresChao objectAtIndex:[pickerView selectedRowInComponent:1]] isEqual: @"Escuro"]) {

        _ImageView_Catalogo.image = [UIImage imageNamed:@"movel_vermelho_chao_escuro.jpg"];

    }
    else if ([[CoresMoveis objectAtIndex:[pickerView selectedRowInComponent:0]] isEqual: @"Castanho"] || [[CoresChao objectAtIndex:[pickerView selectedRowInComponent:1]] isEqual: @"Escuro"]) {

        _ImageView_Catalogo.image = [UIImage imageNamed:@"movel_castanho_chao_escuro.jpg"];

    }
    else if ([[CoresMoveis objectAtIndex:[pickerView selectedRowInComponent:0]] isEqual: @"Cinzento"] || [[CoresChao objectAtIndex:[pickerView selectedRowInComponent:1]] isEqual: @"Escuro"]) {

        _ImageView_Catalogo.image = [UIImage imageNamed:@"movel_cinzento_chao_escuro.jpg"];

    }
    else if ([[CoresMoveis objectAtIndex:[pickerView selectedRowInComponent:0]] isEqual: @"Branco"] || [[CoresChao objectAtIndex:[pickerView selectedRowInComponent:1]] isEqual: @"Escuro"]) {

        _ImageView_Catalogo.image = [UIImage imageNamed:@"movel_branco_chao_escuro.jpg"];

    }
    else if ([[CoresMoveis objectAtIndex:[pickerView selectedRowInComponent:0]] isEqual: @"Vermelho"] || [[CoresChao objectAtIndex:[pickerView selectedRowInComponent:1]] isEqual: @"Claro"]) {

        _ImageView_Catalogo.image = [UIImage imageNamed:@"movel_vermelho_chao_claro.jpg"];

    }
    else if ([[CoresMoveis objectAtIndex:[pickerView selectedRowInComponent:0]] isEqual: @"Castanho"] || [[CoresChao objectAtIndex:[pickerView selectedRowInComponent:1]] isEqual: @"Claro"]) {

        _ImageView_Catalogo.image = [UIImage imageNamed:@"movel_castanho_chao_claro.jpg"];

    }
    else if ([[CoresMoveis objectAtIndex:[pickerView selectedRowInComponent:0]] isEqual: @"Cinzento"] || [[CoresChao objectAtIndex:[pickerView selectedRowInComponent:1]] isEqual: @"Claro"]) {

        _ImageView_Catalogo.image = [UIImage imageNamed:@"movel_cinzento_chao_claro.jpg"];

    }
    else if ([[CoresMoveis objectAtIndex:[pickerView selectedRowInComponent:0]] isEqual: @"Branco"] || [[CoresChao objectAtIndex:[pickerView selectedRowInComponent:1]] isEqual: @"Claro"]) {

        _ImageView_Catalogo.image = [UIImage imageNamed:@"movel_branco_chao_claro.jpg"];
    }
}
4

1 回答 1

1

代替

isEqual:@"Preto"

利用:

isEqualToString:@"Preto"

所以它会是这样的:

if ([[CoresMoveis objectAtIndex:[pickerView selectedRowInComponent:0]] isEqualToString: @"Preto"] || [[CoresChao objectAtIndex:[pickerView selectedRowInComponent:1]] isEqualToString: @"Escuro"]) {

        _ImageView_Catalogo.image = [UIImage imageNamed:@"movel_preto_chao_escuro.jpg"];

    }
else if ([[CoresMoveis objectAtIndex:[pickerView selectedRowInComponent:0]] isEqualToString: @"Vermelho"] || [[CoresChao objectAtIndex:[pickerView selectedRowInComponent:1]] isEqualToString: @"Escuro"]) {

    _ImageView_Catalogo.image = [UIImage imageNamed:@"movel_vermelho_chao_escuro.jpg"];

}
else if ([[CoresMoveis objectAtIndex:[pickerView selectedRowInComponent:0]] isEqualToString: @"Castanho"] || [[CoresChao objectAtIndex:[pickerView selectedRowInComponent:1]] isEqualToString: @"Escuro"]) {

    _ImageView_Catalogo.image = [UIImage imageNamed:@"movel_castanho_chao_escuro.jpg"];

}
else if ([[CoresMoveis objectAtIndex:[pickerView selectedRowInComponent:0]] isEqualToString: @"Cinzento"] || [[CoresChao objectAtIndex:[pickerView selectedRowInComponent:1]] isisEqualToString: @"Escuro"]) {

    _ImageView_Catalogo.image = [UIImage imageNamed:@"movel_cinzento_chao_escuro.jpg"];

}
else if ([[CoresMoveis objectAtIndex:[pickerView selectedRowInComponent:0]] isEqualToString: @"Branco"] || [[CoresChao objectAtIndex:[pickerView selectedRowInComponent:1]] isEqualToString: @"Escuro"]) {

    _ImageView_Catalogo.image = [UIImage imageNamed:@"movel_branco_chao_escuro.jpg"];

}
else if ([[CoresMoveis objectAtIndex:[pickerView selectedRowInComponent:0]] isEqualToString: @"Vermelho"] || [[CoresChao objectAtIndex:[pickerView selectedRowInComponent:1]] isEqualToString: @"Claro"]) {

    _ImageView_Catalogo.image = [UIImage imageNamed:@"movel_vermelho_chao_claro.jpg"];

}
else if ([[CoresMoveis objectAtIndex:[pickerView selectedRowInComponent:0]] isEqualToString: @"Castanho"] || [[CoresChao objectAtIndex:[pickerView selectedRowInComponent:1]] isEqualToString: @"Claro"]) {

    _ImageView_Catalogo.image = [UIImage imageNamed:@"movel_castanho_chao_claro.jpg"];

}
else if ([[CoresMoveis objectAtIndex:[pickerView selectedRowInComponent:0]] isEqualToString: @"Cinzento"] || [[CoresChao objectAtIndex:[pickerView selectedRowInComponent:1]] isEqualToString: @"Claro"]) {

    _ImageView_Catalogo.image = [UIImage imageNamed:@"movel_cinzento_chao_claro.jpg"];

}
else if ([[CoresMoveis objectAtIndex:[pickerView selectedRowInComponent:0]] isEqualToString: @"Branco"] || [[CoresChao objectAtIndex:[pickerView selectedRowInComponent:1]] isEqualToString: @"Claro"]) {

    _ImageView_Catalogo.image = [UIImage imageNamed:@"movel_branco_chao_claro.jpg"];
}

希望这有效!祝你好运!

于 2013-10-14T12:36:34.773 回答