ios UITableView 中的 UIPickerView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19646822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:39:22  来源:igfitidea点击:

UIPickerView in UITableView

iosobjective-ciphoneuitableviewuipickerview

提问by Mike_NotGuilty

First of all: I know that some people already posted topics like that, but nobody gave a clear answer in all those topics.

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

I have a settings-UITableView in my application. Now i want to add a TableViewCell where i can choose between some numbers with a UIPickerView.

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

In the tableView didSelectRowAtIndexPath method i created the if statement for the right cell

在 tableView didSelectRowAtIndexPath 方法中,我为正确的单元格创建了 if 语句

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

    if (indexPath.row == 1){

    }
}

How can i add a UIPickerView for this cell? It should slide up from the bottom and it would be nice if there is something like a "done" button.

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

回答by DaiMaoTsai

Have you seen the sample program released with iOS 7 called DateCell? It uses a UIDatePicker, but I found it pretty straightforward to adapt it to a regular UIPickerView.

你看过 iOS 7 发布的示例程序DateCell吗?它使用 UIDatePicker,但我发现将其调整为常规 UIPickerView 非常简单。

It does exactly what you're describing: edit a UITableViewCell with a picker that opens directly under the row you're editing.

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

回答by Unheilig

This is also based on your other question, so I am including those functionalities here as well (i.e. the switch).

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

In your YourTableViewController.h

在你的YourTableViewController.h

set:

放:

@interface YourTableViewViewController : UITableViewController <UIPickerViewDataSource, UIPickerViewDelegate>

In your YourTableViewController.m

在你的YourTableViewController.m

Paste this code:

粘贴此代码:

@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.

Addendum:

附录:

Also, in your storyboard, change the separatorfor YourTableViewto None(looks better in your case here).

此外,在你的故事板,改变separatorYourTableView(看起来你的情况更好地在这里)。

Table View Separator:

表视图分隔符:

in viewDidLoad, add:

viewDidLoad,添加:

self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;

And in bringUpPickerViewWithRow

而在 bringUpPickerViewWithRow

after:

后:

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

add:

添加:

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

And lastly, in hidePickerView, add:

最后,在 中hidePickerView,添加:

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

回答by HDG

  1. Create custom ViewController that include UIPickerView and Done button.
  2. Add it as subview on your tableview. (Place it bottom so that it does not visible).
  3. Once user click appropriate cell, you can animate picker-view from bottom.
  4. Once user select an item animate and position it to bottom.
  1. 创建包含 UIPickerView 和完成按钮的自定义 ViewController。
  2. 将其添加为 tableview 上的子视图。(将其放在底部,使其不可见)。
  3. 一旦用户单击适当的单元格,您就可以从底部动画选择器视图。
  4. 一旦用户选择一个项目动画并将其定位到底部。

I have done above steps for same purpose. If you want I can send custom controller that include UIPickerView and done button.

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