首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >单击tablecell显示UIDatePicker

单击tablecell显示UIDatePicker
EN

Stack Overflow用户
提问于 2012-09-12 23:01:24
回答 1查看 6.8K关注 0票数 2

我有一个tableview控制器,其中一个单元格是"date“,当我单击时,我想显示一个datepicker并更新值。

当我点击limit date时,我会看到

下面是代码:

代码语言:javascript
复制
-(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顶部的黑条是什么?我如何删除它?

第二:当用户在datepicker外部单击时,如何关闭datepicker?

第三:如何移动表视图滚动条,以便在datepicker打开时"limit date“字段可见?

抱歉截图太大了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-12 23:15:15

presenting your date picker modally in UIActionSheet呢?

在上面的例子中,黑条(这可能是一个大小问题)被一个方便的工具栏所取代,在这个工具栏中,你可以放置像“保存”和“取消”这样的按钮,我相信如果你使用一个UIActionSheet并在它外面单击,它会自动消失(我不确定这一点),但如果‘它不是,你可以使用它的dismiss方法来隐藏它。

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12391129

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档