0

I establish a tableview , but I want to click direct cell show the picker to select.

Then the selected value will show in the cell label component.

But I had try to labelName.text = picker. or call picker in the didSelectRowAtIndexPath . It's not showing the picker.

my code is below

    -(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"MotDeteTableViewCell";
        cell = (MotDeteTableViewCell*)[tableView  dequeueReusableCellWithIdentifier:CellIdentifier];

        if( cell == nil )
        {
            cell = [[MotDeteTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        }
        cell.motTableTitleLabel.text = [tableDatasAry  objectAtIndex:indexPath.row];

        if( indexPath.row == 0)
        {

      }
        else if ( indexPath.row == 1)
        {
            myLb = [ [UILabel alloc ] initWithFrame:CGRectMake(_motTableView.frame.size.width -50 , 0.0, 70.0, 40.0) ];

                 // -------  I expect click the picker value will show in myLb label
            myLb.text = @"picker will show value"; 

            [cell addSubview:myLb];

        }



        return cell;
    }


    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    switch (indexPath.row) {


            case 0:
                break;

            case 1:
                 // -------- I had try put settingPickeProperty in here or viewdidload
                [self settingPickeProperty];

                break;
          default:
                break;
        }
    }

    #pragma mark -  settting picker datas
    -(void) settingPickeProperty
    {
        picker = [[UIPickerView alloc] initWithFrame:CGRectZero];

        picker  .delegate = self;
        picker  .dataSource = self;
        [ picker  setShowsSelectionIndicator:YES];

        // -------if i use textfield the picker had correct show. but i want to click        cell show the picker, I don't know how to set  xxxx = picker;
        _pickertf.inputView =  picker  ;

        UIToolbar*  mypickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 56)];
        mypickerToolbar.barStyle = UIBarStyleDefault;
        [mypickerToolbar sizeToFit];

        NSMutableArray *barItems = [[NSMutableArray alloc] init];

        UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc]         initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
       [barItems addObject:flexSpace];

        UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"done" style:UIBarButtonItemStyleBordered target:self action:@selector(pickerDoneClicked)];


        [barItems addObject:doneBtn];
        [mypickerToolbar setItems:barItems animated:YES];
        // ------ if use textfield had correct...
        _pickertf.inputAccessoryView = mypickerToolbar;

    }


    -(void)pickerDoneClicked
    {
        NSLog(@"Done Clicked");

    }

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


    - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:        (NSInteger)component
    {
        return [titleAry count];
    }


    - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
    {
        return [titleAry objectAtIndex:row];
    }


    - (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    {

    }

It's my code offer every reference.

How to achieve click tableview cell to show the picker then select picker value show in myLb?

Kromster
  • 7,181
  • 7
  • 63
  • 111
dickfala
  • 3,246
  • 3
  • 31
  • 52

2 Answers2

0

You are not adding the pickerView anywhere.

If you want to add in the current view, just use

picker = [[UIPickerView alloc] initWithFrame:CGRectZero];

picker.delegate = self;
picker.dataSource = self;
[picker setShowsSelectionIndicator:YES];
[self.view addSubView:picker];
Himanshu Joshi
  • 3,391
  • 1
  • 19
  • 32
  • If I set textfield . the picker will show when i click the textfield. ( _pickertf.inputView = picker ;) But If I will set on tableview cell when user click. I don't know how to set this part. And I had try to add [self.view addSubView:picker]; it's will show error. below: No visible @interface for 'UIView' declares the selector 'addSubView:' thanks – dickfala Mar 06 '14 at 07:14
  • What is the base class in which you are adding the picker? – Himanshu Joshi Mar 06 '14 at 07:15
  • the effect is click picker will show. not always show on the view. – dickfala Mar 06 '14 at 07:15
  • my base class is extends UIViewController – dickfala Mar 06 '14 at 07:16
  • So what is this **No visible @interface for 'UIView' declares the selector 'addSubView:'** ? – Himanshu Joshi Mar 06 '14 at 07:21
  • Did you used UIPicker `delegates` and `datasource` protocols? – Himanshu Joshi Mar 06 '14 at 07:22
  • yes, I have add protocols. If i try to use textfield, the picker will show when i click textfield. So the picker program maybe not problem. The problem is on _pickertf.inputView = picker ; here. If i use textfield , the picker is set _pickertf.inputview = picker. If I want to set click on the cell. I don't know how to set xxx = picker or other implement method. thank you~ – dickfala Mar 06 '14 at 07:26
  • _pickertf is my test textfield. – dickfala Mar 06 '14 at 07:31
  • So basically you want the selected value from picker in textfield? – Himanshu Joshi Mar 06 '14 at 07:35
  • NO.. I have a label in the tableview cell. I want to click tableview cell jump to picker . Then after select picker value click done. It's will show value in the label. TextField(_pickertf) just I try to know picker program is correct or not. – dickfala Mar 06 '14 at 07:39
0

According to my understanding, you need to show picker value in selected table cell label.

So following steps may help you if cells are constant; for example just three cells.

You just not need to dequeue the cell and then try to set the value.

msmq
  • 1,298
  • 16
  • 28