How can I make my UIPickerView show labels after the selected value?

后端 未结 3 1098
心在旅途
心在旅途 2021-01-03 23:44

Right now, the selecter just shows the data being selected. I want it to have a word after the selected value, the same way the iPhone clock app has \"hours\" and \"min\" a

3条回答
  •  日久生厌
    2021-01-04 00:32

    Here, I got proper answer for this question(I have did it for one component change it as per your requirement) take a look: you need to implement the method:

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

    Take a int variable in .h file named lastSelectedIndex

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

    I have tested it. Hope it will help you.... :)

提交回复
热议问题