简体   繁体   English

单击表单元显示UIDatePicker

[英]Click on tablecell show UIDatePicker

I have a tableview controller and one of the cell is "date" and when i click i wanna show a datepicker and update the values. 我有一个tableview控制器,单元格之一是“日期”,当我单击时,我想显示一个日期选择器并更新值。

带日期单元格的表

When i click on limit date i get 当我单击限制日期时,我得到

带有日期选择器的表

Now here is the code: 现在这里是代码:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ( indexPath.section == 0 )
    {
        if ( indexPath.row == 3 )
        {
            UITableViewCell *targetCell = [tableView cellForRowAtIndexPath:indexPath];
            self.pickerView.date = [self.dateFormatter dateFromString:targetCell.detailTextLabel.text];

            // check if our date picker is already on screen
            if (self.pickerView.superview == nil)
            {
                [self.view.window addSubview: self.pickerView];
                // size up the picker view to our screen and compute the start/end frame origin for our slide up animation
                //
                // compute the start frame

                CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
                CGSize pickerSize = [self.pickerView sizeThatFits:CGSizeZero];
                CGRect startRect = CGRectMake(0.0,
                                              screenRect.origin.y + screenRect.size.height,
                                              pickerSize.width, pickerSize.height);
                self.pickerView.frame = startRect;

                // compute the end frame
                CGRect pickerRect = CGRectMake(0.0,
                                               screenRect.origin.y + screenRect.size.height - pickerSize.height,
                                               pickerSize.width,
                                               pickerSize.height);
                // start the slide up animation
                [UIView beginAnimations:nil context:NULL];
                    [UIView setAnimationDuration:0.3];

                    // we need to perform some post operations after the animation is complete
                    [UIView setAnimationDelegate:self];

                    self.pickerView.frame = pickerRect;

                    // shrink the table vertical size to make room for the date picker
                    CGRect newFrame = self.tableView.frame;
                    newFrame.size.height -= self.pickerView.frame.size.height;
                    self.tableView.frame = newFrame;
                [UIView commitAnimations];

                // add the "Done" button to the nav bar

                self.navigationItem.rightBarButtonItem = self.doneButton;
            }
        }
    }
}


- (IBAction)dateChanged:(id)sender
{
    [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0]].detailTextLabel.text = [self.dateFormatter stringFromDate:[sender date]];
}

- (void)slideDownDidStop
{
    [self.pickerView removeFromSuperview];
}

- (IBAction)doneAction:(id)sender
{
    CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
    CGRect endFrame = self.pickerView.frame;
    endFrame.origin.y = screenRect.origin.y + screenRect.size.height;

    // start the slide down animation
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];

    // we need to perform some post operations after the animation is complete
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(slideDownDidStop)];

    self.pickerView.frame = endFrame;
    [UIView commitAnimations];

    // grow the table back again in vertical size to make room for the date picker
    CGRect newFrame = self.tableView.frame;
    newFrame.size.height += self.pickerView.frame.size.height;
    self.tableView.frame = newFrame;

    // remove the "Done" button in the nav bar
    self.navigationItem.rightBarButtonItem = nil;

    // deselect the current table row
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];

    self.navigationItem.rightBarButtonItem = self.nextButton;
}

I have three questions: 我有三个问题:

First: what is that black bar on top of datepicker and how do i remove it? 第一:日期选择器顶部的黑条是什么?如何删除?

Second: what can i do for closing the datepicker when the user clicks outside the datepicker? 第二:当用户在日期选择器外部单击时,如何关闭日期选择器?

Third: How can i move the tableview scroll so that the field "limit date" can be visible when datepicker opens? 第三:如何打开表格视图滚动条,以便在打开日期选择器时可以看到“限制日期”字段?

Sorry for the big screenshots. 很抱歉获得大屏幕截图。

What about presenting your date picker modally in UIActionSheet ? 如何在UIActionSheet中以模态显示日期选择器呢?

In the example above that black bar (which is probably a sizing issue) is replaced with a convenient toolbar in which you can place buttons like "save" and "cancel", and I believe that if you use a UIActionSheet and click outside of it it's automatically dismissed (I'm not sure about this), but if' it's not you can use it's dismiss method to hide it. 在上面的示例中,黑条(可能是大小问题)被便捷的工具栏代替,您可以在其中放置“保存”和“取消”之类的按钮,我相信如果您使用UIActionSheet并在其外部单击它会自动关闭(我不确定),但是如果不是,则可以使用它的dismiss方法将其隐藏。

About the table view, you can use [myTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:someRow inSection:someSection] atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; 关于表视图,可以使用[myTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:someRow inSection:someSection] atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; to set the offset of the table scroll at a particular cell you want. 在所需的特定单元格上设置表格滚动的偏移量。

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

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