我可以点击tableview单元格显示选择器吗?

时间:2014-03-06 06:44:45

标签: ios label tableview cell uipickerview

我建立了一个tableview,但我想点击直接单元格显示选择器。

然后,所选值将显示在单元格标签组件中。

但我试过labelName.text = picker。或者在didSelectRowAtIndexPath中致电选择器。它没有显示选择器。

我的代码在

之下
    -(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
    {

    }

这是我的代码提供的每一个参考。

如何实现点击tableview单元格以显示选择器,然后在myLb中选择选择器值显示?

2 个答案:

答案 0 :(得分:0)

您没有在任何地方添加pickerView

如果要添加当前视图,请使用

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

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

答案 1 :(得分:0)

根据我的理解,您需要在选定的表格单元格标签中显示选择器值。

如果细胞是恒定的,那么下面的步骤可能会有所帮助;例如,只有三个细胞。

您不需要将单元格出列,然后尝试设置值。