【问题标题】:Click on tablecell show UIDatePicker点击 tablecell 显示 UIDatePicker
【发布时间】:2012-09-12 15:01:24
【问题描述】:

我有一个表格视图控制器,其中一个单元格是“日期”,当我单击时,我想显示一个日期选择器并更新值。

当我点击限制日期时,我得到

现在是代码:

-(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;
}

我有三个问题:

首先:datepicker 顶部的黑条是什么,我该如何移除它?

第二:当用户在日期选择器之外点击时,我可以做些什么来关闭日期选择器?

第三:如何移动tableview滚动,以便在打开日期选择器时可以看到“限制日期”字段?

对不起,大屏幕截图。

【问题讨论】:

    标签: ios uitableview uidatepicker tablecell


    【解决方案1】:

    presenting your date picker modally in UIActionSheet 呢?

    在上面的示例中,黑色条(这可能是大小问题)被一个方便的工具栏替换,您可以在其中放置“保存”和“取消”等按钮,我相信如果您使用 UIActionSheet并在其外部单击它会自动关闭(我不确定),但如果不是,您可以使用它的 dismiss 方法来隐藏它。

    关于表格视图,您可以使用[myTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:someRow inSection:someSection] atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; 设置表格滚动到您想要的特定单元格的偏移量。

    【讨论】:

    • 黑条来自缩小表格视图并显示窗口背景。不需要缩小表格视图,因为 UIDatePicker 位于表格上方,因为它后来被添加到容器视图中。
    猜你喜欢
    • 2012-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多