我该如何在UIPickerView中放置一张图片?

4

我不知道如何创建一个带有图像的自定义UIPickerView。

我一直在寻找方法,最近找到了这个:

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view

但我不知道该怎么使用。

这是我的数组:
所有的都运作得很完美,我只想在每一行中放入不同的图像,有10种不同的颜色。 这些颜色对于这3个组件来说是相同的,但在每一行中是不同的。 就像这张图片。链接!!!

- (void)viewDidLoad
{
    [super viewDidLoad];

    Colores1 = [[NSMutableArray alloc] init];
    [Colores1 addObject:@"Negro"];
    [Colores1 addObject:@"Marrón"];
    [Colores1 addObject:@"Rojo"];
    [Colores1 addObject:@"Naranja"];
    [Colores1 addObject:@"Amarillo"];
    [Colores1 addObject:@"Verde"];
    [Colores1 addObject:@"Azul"];
    [Colores1 addObject:@"Violeta"];
    [Colores1 addObject:@"Gris"];
    [Colores1 addObject:@"Blanco"];

    Colores2 = [[NSMutableArray alloc] init];
    [Colores2 addObject:@"Negro"];
    [Colores2 addObject:@"Marrón"];
    [Colores2 addObject:@"Rojo"];
    [Colores2 addObject:@"Naranja"];
    [Colores2 addObject:@"Amarillo"];
    [Colores2 addObject:@"Verde"];
    [Colores2 addObject:@"Azul"];
    [Colores2 addObject:@"Violeta"];
    [Colores2 addObject:@"Gris"];
    [Colores2 addObject:@"Blanco"];

    Colores3 = [[NSMutableArray alloc] init];
    [Colores3 addObject:@"Negro"];
    [Colores3 addObject:@"Marrón"];
    [Colores3 addObject:@"Rojo"];
    [Colores3 addObject:@"Naranja"];
    [Colores3 addObject:@"Amarillo"];
    [Colores3 addObject:@"Verde"];
    [Colores3 addObject:@"Azul"];
    [Colores3 addObject:@"Violeta"];
    [Colores3 addObject:@"Gris"];
    [Colores3 addObject:@"Blanco"];
}

这是我的UIPickerView:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{

    if(component == Primbanda)
        return [Colores1 count];
    if(component == Segubanda)
        return [Colores2 count];
    if (component == tercbanda)
        return [Colores3 count];
    return 0;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if(component == Primbanda)
        return [Colores1 objectAtIndex:row];
    if(component == Segubanda)
        return [Colores2 objectAtIndex:row];
    if (component == tercbanda) 
        return [Colores3 objectAtIndex:row];
    return 0;

}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSLog(@"Row is:%d Component is:%d",row,component);

    int a;//entero de componente 1
    int b;//entero de componente 2
    double c;//entero de componente 3
    double resultado;
    int d; //valor de componente 1 y 2
    NSString *a1; //cadena de a y b!

    a = [pickerView selectedRowInComponent:0]; //asigno a las variables segun el numero!! 
    b = [pickerView selectedRowInComponent:1];

    switch ([pickerView selectedRowInComponent:2]) { //asi se le asignan valores propios!
        case 0:
            c=1;
            break;
        case 1:
            c=10;
            break;
        case 2:
            c=100;
            break;
        case 3:
            c=1000;
            break;
        case 4:
            c=10000;
            break;
        case 5:
            c=100000;
            break;
        case 6:
            c=1000000;
            break;
        case 7:
            c=10000000;
            break;
        case 8:
            c=100000000;
            break;
        case 9:
            c=1000000000;
            break;
        default:
            break;
    }
    NSLog(@"El valor de la tercera es ;%f",c);
    a1 = [[NSString alloc]initWithFormat:@"%d%d",a,b]; //junto las variables a y b
    d= [a1 intValue];    //guardo en la variable
    NSLog(@"el valor de C multiplicado es %f",c);

    resultado = d*c; //multiplica ab por el valor de c

    NSLog(@"valor de resistencia es %.0f Ohms",resultado); //awebo! me salio! :D

    labelresultado.text = [[NSString alloc]initWithFormat:@"%.0f",resultado];

}

您提供的图片链接需要登录才能访问。请使用编辑器中的图标上传图片。 - houbysoft
2个回答

2

UIPickerViewDelegate文档中得知:

pickerView:viewForRow:forComponent:reusingView:

Called by the picker view when it needs the view to use for a given row in a given component.
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
Parameters

pickerView

    An object representing the picker view requesting the data.
row

    A zero-indexed number identifying a row of component. Rows are numbered top-to-bottom.
component

    A zero-indexed number identifying a component of pickerView. Components are numbered left-to-right.
view

    A view object that was previously used for this row, but is now hidden and cached by the picker view.

重要的是返回的视图,这是用于在选择器视图上显示该行内容的视图,并且您希望在其中添加任何图像的地方。

首先,您必须实现委托方法,并在.h文件中声明如下:

MyClass <UIPickerViewDelegate>

在你的 .m 文件中。
myUIPickerObject.delegate = self;

那么请按照以下方式实现该方法:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UIView *myColorView = [UIView new]; //Set desired frame
    myColorView.backgroundColor = [UIColor redColor]; //Set desired color or add a UIImageView if you want...
    UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:"myImage.png"]];
    [view addSubview:myImageView];
    [view addSubview:myColorView];

    //Add subview retains view, so OK to release now
    [myImageView release];
    [myColorView release];

    return view;
}

希望这能对您有所帮助。

但是这种方法不会用行标题方法替换每行中之前显示的文本吗? - Jose Garfias Lopez Parkinson
除非您将子视图设置太大,否则它不会简单地将框架设置为放置图像的视图的某个部分。然后您将能够看到文本和图像。 - Oscar Gomez
是的!好的,谢谢你,但是Xcode在“视图”部分显示: [view addSubView:myImageView]; [view addSubView:myColorView]; “没有可见的@接口为uiview声明选择器addSubView” ... >.< 我是新手.. 帮帮我!拜托了。 - Jose Garfias Lopez Parkinson
它是addSubview而不是addSubView。 - Trent

1
这段代码对我来说可行:

-(void) viewDidLoad{
    [super viewDidLoad];
    // Initialize Data
    _pickerData = @[@"Item 1", @"Informatio", @"AUx2 ", @"Services 061", @"Problems"];

    // Connect data
    self.tipoConsulta.dataSource = self;
    self.tipoConsulta.delegate = self;
}

- (int)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// The number of rows of data
- (int)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return _pickerData.count;
}

// The data to return for the row and component (column) that's being passed in
- (UIView*)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UIView *pickerCustomView = [UIView new];

    UILabel *mytext = [[UILabel alloc] init];
    mytext.text = _pickerData[row];
    [mytext setFrame:CGRectMake(60,0,50,50)];
    [pickerCustomView addSubview:mytext];
    UIImageView *myIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ic_tipo_servicio061_rounded.png"]];
    [myIcon setFrame:CGRectMake(0, 0, 50, 50)];

    [pickerCustomView addSubview:myIcon];
    [pickerCustomView addSubview:mytext];
    return pickerCustomView;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接