【问题标题】:How can I make my UIPickerView show labels after the selected value?如何让我的 UIPickerView 在选定值之后显示标签?
【发布时间】:2023-03-18 22:46:01
【问题描述】:

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

【问题讨论】:

    标签: iphone ios objective-c cocoa-touch


    【解决方案1】:

    在这里,我得到了这个问题的正确答案(我已经为一个 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 文件中获取 int 变量

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

    我已经测试过了。希望对您有所帮助.... :)

    【讨论】:

    • @Vivek Sehrawat :- 嗨,看看它,这是你的解决方案。 :)
    【解决方案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;
    }
    

    【讨论】:

    • 这会在每一行上创建标签,而不仅仅是选定的行。见a better way
    • @Gypsa- 你的答案在每一行上都有一个 uilabel,而不是在一个选定的指标上
    【解决方案3】:

    你必须做一个定制的UIPickerView

    如果你需要的只是添加一些标签,你可以简单地添加到UIPickerView 两个UILabel 上,因为UIPickerView 一个UIView...快速但肮脏...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-14
      • 2014-05-14
      • 2011-06-15
      • 2018-11-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多