简体   繁体   English

UITableView中的UIPickerView

[英]UIPickerView in UITableView

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. 我的应用程序中有一个设置-UITableView。 Now i want to add a TableViewCell where i can choose between some numbers with a UIPickerView. 现在我想添加一个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? 如何为此单元格添加UIPickerView? It should slide up from the bottom and it would be nice if there is something like a "done" button. 它应该从底部向上滑动,如果有类似“完成”按钮的话会很好。

Have you seen the sample program released with iOS 7 called DateCell ? 您是否看过iOS 7发布的示例程序名为DateCell It uses a UIDatePicker, but I found it pretty straightforward to adapt it to a regular UIPickerView. 它使用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。

This is also based on your other question, so I am including those functionalities here as well (ie 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 separator for YourTableView to None (looks better in your case here). 此外,在您的故事板中 ,将YourTableViewseparator YourTableView (在此情况下看起来更好)。

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];
  1. Create custom ViewController that include UIPickerView and Done button. 创建包含UIPickerView和Done按钮的自定义ViewController。
  2. Add it as subview on your tableview. 在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. 一旦用户选择一个项目动画并将其定位到底部。

I have done above steps for same purpose. 为了同样的目的,我已完成上述步骤。 If you want I can send custom controller that include UIPickerView and done button. 如果你想我可以发送包含UIPickerView和完成按钮的自定义控制器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM