UITableView中的UIPickerView

首先:我知道有些人已经发表了这样的话题,但是没有人在所有这些话题上给出明确的答案。

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

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

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.row == 1){ } } 

我怎样才能为这个单元格添加一个UIPickerView? 它应该从底部向上滑动,如果像“完成”button那样会很好。

这也是基于你的其他问题,所以我在这里也包括这些function(即交换机)。

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. 

附录:

此外,在故事板中 ,将YourTableViewseparator YourTableView (在这里看起来更好)。

表视图分隔符:

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]; 

你有没有看到与iOS 7发布的示例程序称为DateCell ? 它使用了一个UIDatePicker,但是我发现它很容易适应一个普通的UIPickerView。

它的确如你所描述的那样:用一个select器编辑一个UITableViewCell,直接在你正在编辑的行下打开。

  1. 创build包含UIPickerView和完成button的自定义ViewController。
  2. 将它作为子视图添加到你的tableview中。 (把它放在底部,使其不可见)。
  3. 一旦用户点击适当的单元格,就可以从底部为拾取器视图制作animation。
  4. 一旦用户select一个项目animation,并将其放置在底部。

为了同样的目的我已经完成了上述步骤。 如果你想我可以发送包含UIPickerView和完成button的自定义控制器。