【问题标题】:UIPickerView in UITableViewUITableView 中的 UIPickerView
【发布时间】:2013-10-28 23:16:30
【问题描述】:

首先:我知道有些人已经发布了类似的主题,但没有人对所有这些主题给出明确的答案。

我的应用程序中有一个设置-UITableView。现在我想添加一个 TableViewCell,我可以在其中使用 UIPickerView 在一些数字之间进行选择。

在 tableView didSelectRowAtIndexPath 方法中,我为右侧单元格创建了 if 语句

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

    if (indexPath.row == 1){

    }
}

如何为这个单元格添加 UIPickerView?它应该从底部向上滑动,如果有类似“完成”按钮的东西会很好。

【问题讨论】:

  • 我不确定您为什么要这样做,但似乎您应该使用 tableview 中的部分而不是 UITableView 单元格中的 UIPickerview。您正在谈论的动画(从底部向上滑动)已准备好使用 UIActionSheet 烘焙。因此,您可能有表格单元格,当单击这些单元格时,会显示带有 UIPickerView 的 UIActionSheet。
  • 是的,这就是我想要的。我想当我点击 tableviewcell 时,选择器应该从下往上滑动。然后我选择一个值,按完成,选择器消失!
  • 你可以使用我的图书馆:github.com/hijamoya/PickerViewCell

标签: ios objective-c iphone uitableview uipickerview


【解决方案1】:

您是否看过随 iOS 7 发布的名为 DateCell 的示例程序?它使用了一个 UIDatePicker,但我发现它很容易适应常规的 UIPickerView。

它完全符合您的描述:编辑 UITableViewCell 并使用直接在您正在编辑的行下方打开的选择器。

【讨论】:

    【解决方案2】:

    这也是基于您的其他问题,因此我也将这些功能包括在此处(即开关)。

    在您的 YourTableViewController.h

    设置:

    @interface YourTableViewViewController : UITableViewController <UIPickerViewDataSource, UIPickerViewDelegate>
    

    在您的 YourTableViewController.m

    粘贴此代码:

    @interface YourTableViewViewController ()
    @property NSInteger toggle;
    @property (strong, nonatomic) UIPickerView *pickerView;
    @end
    
    @implementation YourTableViewViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        self.toggle = 0;
        self.pickerView = [[UIPickerView alloc] initWithFrame:(CGRect){{0, 0}, 320, 480}];
        self.pickerView.delegate = self;
        self.pickerView.dataSource = self;
        self.pickerView.center = (CGPoint){160, 640};
        self.pickerView.hidden = YES;
        [self.view addSubview:self.pickerView];
    }
    
    #pragma mark - Table view data source
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        // Return the number of rows in the section.
        return 2;    
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
        if(cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        if(indexPath.row == 0)
        {
            [cell.contentView addSubview:self.mySwitch];
        }
        if(indexPath.row == 1)
        {
            cell.textLabel.textColor = [UIColor darkGrayColor];
            if(self.toggle == 0)
            {
                cell.textLabel.text = @"Choose a number";
            }
            else
            {
                cell.textLabel.text = @"Cancel";
            }
        }
        // Configure the cell...
    
        return cell;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if(indexPath.row == 1)
        {
            if(self.toggle == 0)
            {
                self.toggle = 1;
                [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:YES];
                [self bringUpPickerViewWithRow:indexPath];
            }
            else
            {
                self.toggle = 0;
                [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:YES];
                [self hidePickerView];
            }
        }
    }
    
    - (void)bringUpPickerViewWithRow:(NSIndexPath*)indexPath
    {
        UITableViewCell *currentCellSelected = [self.tableView cellForRowAtIndexPath:indexPath];
        [UIView animateWithDuration:1.0f
                              delay:0.0f
                            options:UIViewAnimationOptionCurveEaseInOut
                         animations:^
                         {
                             self.pickerView.hidden = NO;
                             self.pickerView.center = (CGPoint){currentCellSelected.frame.size.width/2, self.tableView.frame.origin.y + currentCellSelected.frame.size.height*4};
                         }
                         completion:nil];
    }
    
    - (void)hidePickerView
    {
        [UIView animateWithDuration:1.0f
                              delay:0.0f
                            options:UIViewAnimationOptionCurveEaseInOut
                         animations:^
         {
             self.pickerView.center = (CGPoint){160, 800};
         }
                         completion:^(BOOL finished)
         {
             self.pickerView.hidden = YES;
         }];
    }
    
    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    {
        self.toggle = 0;
        [self.tableView reloadData];
        [self hidePickerView];
        NSLog(@"row selected:%ld", (long)row);
    }
    
    - (NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
    {
        return [NSString stringWithFormat:@"%d", row+1];
    }
    
    - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
    {
        return 1;
    }
    
    - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
    {
        return 10;
    }
    
    @end
    
    Tested.
    

    附录:

    另外,在您的 故事板 中,将 separatorYourTableView 更改为 None(在您的情况下看起来更好)。

    表格视图分隔符:

    viewDidLoad,添加:

    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    

    bringUpPickerViewWithRow

    之后:

    UITableViewCell *currentCellSelected = [self.tableView cellForRowAtIndexPath:indexPath];
    

    添加:

    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView setNeedsDisplay];
    

    最后,在hidePickerView 中添加:

    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.tableView setNeedsDisplay];
    

    【讨论】:

    • 感谢您的回答!我在self.pickerView = CGPoint ... 不兼容类型CGPoint(又名结构CGPoint)的bringUpPickerViewWithRow 方法中遇到错误。你知道为什么吗?
    • 有效!谢谢!我在这个单元格下有更多单元格。选择器有可能不会滑得那么高吗?我只需要编辑这个 self.pickerview.center 行吗?这条线负责选择器滑多高吗? ...好的,是的,就是那条线,哈哈。 :-)
    • @user2710855 是的,试试这条线,你会得到你想要的高度。
    • 最后一个问题:是否有可能当我点击单元格时,表格视图分隔线应该消失?那太好了
    • @user2710855 是的,让我在我的回答中补充一下。
    【解决方案3】:
    1. 创建包含 UIPickerView 和 Done 的自定义 ViewController 按钮。
    2. 将它作为子视图添加到您的表格视图中。 (把它放在底部,这样 它不可见)。
    3. 一旦用户单击相应的单元格,您 可以从底部动画选择器视图。
    4. 一旦用户选择一个项目 设置动画并将其置于底部。

    出于同样的目的,我已经完成了上述步骤。如果您愿意,我可以发送包含 UIPickerView 和完成按钮的自定义控制器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多