如何在选定的值后使我的UIPickerView显示标签?

现在,select器只显示正在select的数据。 我希望它在选定的值之后有一个单词,就像iPhone时钟应用程序在select器上有“小时”和“分钟”一样。

在这里输入图像说明

你必须使用下面的方法自己创build两个标签。这是pickerView的委托方法,当pickerview被加载或者当它获得reloadcomponent命令的时候自动调用。

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)theView{ UIView *pickerviewtemp=[[UIView alloc] initWithFrame:CGRectZero]; UILabel *lbl=[[[UILabel alloc] initWithFrame:yourrequiredframe]autorelease]; [lbl setBackgroundColor:[UIColor clearColor]]; [lbl setText:*set text according to yourself*]; [lbl setFont:[UIFont boldSystemFontOfSize:16]]; [pickerviewtemp addSubview:lbl]; UILabel *lb2=[[[UILabel alloc] initWithFrame:yourrequiredframe]autorelease]; [lbl2 setBackgroundColor:[UIColor clearColor]]; [lbl2 setText:*set text according to yourself*]; [lbl2 setFont:[UIFont boldSystemFontOfSize:16]]; [pickerviewtemp addSubview:lbl2]; return pickerviewtemp; } 

在这里,我得到了正确的答案(我已经做了一个component ,根据您的要求更改)看看:您需要实现的方法:

 -(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { //set your View. Here is an example .. UIView *pickerviewtemp=[[UIView alloc] initWithFrame:CGRectZero]; UILabel *lbl=[[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)]autorelease]; [lbl setBackgroundColor:[UIColor clearColor]]; [lbl setText:[array_from objectAtIndex:row]]; [lbl setFont:[UIFont boldSystemFontOfSize:16]]; [pickerviewtemp addSubview:lbl]; return pickerviewtemp; } 

在名为lastSelectedIndex .h文件中取一个intvariables

 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { UILabel *label = (UILabel*)[pickerView viewWithTag:999+(component*lastSelectedIndex)]; // you can set your own tag... [label removeFromSuperview]; UIView *view = [pickerView viewForRow:row forComponent:component]; UILabel *lbl2=[[[UILabel alloc] initWithFrame:CGRectMake(110, 0, 50, 50)]autorelease]; lbl2.tag = 999+(component*row); [lbl2 setBackgroundColor:[UIColor clearColor]]; [lbl2 setText:@yourtext"]; [lbl2 setFont:[UIFont boldSystemFontOfSize:16]]; [view addSubview:lbl2]; lastSelectedIndex = row; } 

我已经testing过了。 希望它会帮助你…. 🙂

你将不得不做一个定制的UIPickerView

在你的情况下,如果你需要的只是添加一些标签,你可以简单地添加到UIPickerView Two UILabel ,因为UIPickerView 一个UIView …快速,但脏…