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

19 iphone cocoa-touch objective-c ios

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

在此输入图像描述

Gyp*_*psa 2

您必须使用以下方法自己创建两个标签。这是 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;
}
Run Code Online (Sandbox Code Playgroud)

  • 这将在每一行上创建标签,而不仅仅是选定的行。请参阅[更好的方法](http://stackoverflow.com/questions/367471/fixed-labels-in-the-selection-bar-of-a-uipickerview)。 (3认同)