带选择器视图的UITableViewController

时间:2010-11-04 11:00:34

标签: iphone uitableview uidatepicker

我有一个严重的问题,我自己无法解决。我花了几个小时搜索文档,编程指南以及开发人员论坛和堆栈溢出。

问题是我想在UITableViewController中显示一个选择器视图。我有一个屏幕,有多个文本字段允许我按标题/作者/关键字搜索...我还想指定最小和最大日期,使用UIDatePicker(或UIPickerView - 指定“最近5天”例如)。

我想使用UITableViewController,因为当用户按下文本字段时键盘弹出时,它会节省大量时间调整表格的时间。事实上,我从来没有能够使用UIViewController重现这个动画并听取textfields的委托。它几乎是完美的,但如果使用UITableViewController显示,则与表的行为相比有一些明显的缺点。

当只有文本字段时,一切都很好。但是日期文件怎么样?当我想要添加新联系人并指定生日时,我想使它与Apple的Contacts.app完全相同。在该应用程序中显示日期选择器,表格调整大小,在电子邮件/电话领域和生日工作之间切换很好。我可以相信日期选择器在这种情况下是键盘,但不是用于键入电话/电子邮件而是日期,因为它像键盘一样滑入/滑出并在键盘/选择器打开时立即更换。

他们是如何完成的? 或者我在哪里可以找到最简单的方法来重现它。我相信它不会那么难,因为这是非常常见的情况。

此致 克里斯

4 个答案:

答案 0 :(得分:7)

所有这一切都毫无意义。我们应该处理inputView和inputAccessoryView,其中inputView应该有picker和inputAccessoryView工具栏。

答案 1 :(得分:1)

您将不得不创建一个UIWindow对象,然后添加一个视图。 windowLevel属性使其高于statusBar,您可能想要也可能不想要它。

//statusWindow is a UIWindow ivar declared in the header
//pickerShowing is declared as a BOOL in header
//release and removeFromSuperview is done in the animation delegate methods

//ANIMATE IN

-(void)slideIn {
    CGRect pickerFrame = CGRectMake(0.0f, 0.0f, 320.0f, 200.0f); //guessing on height
    UIView *viewForPicker = [[UIView alloc] init];
    UIPickerView *aPicker = [[UIPickerView alloc] init]; //don't forget to set delegate and dataSource
    viewForPicker.frame = pickerFrame;
    statusWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0.0, 480.0, 320.0f, 200.0f)];//guessing on height, y value is off the screen (bottom)
    statusWindow.windowLevel = UIWindowLevelStatusBar;
    statusWindow.hidden = NO;
    statusWindow.backgroundColor = [UIColor clearColor];
    [statusWindow makeKeyAndVisible];
    [viewForPicker addSubview:aPicker];
    [statusWindow addSubview:viewForPicker];
    [viewForPicker release];
    [aPicker release];
    [UIView beginAnimations:@"slideUp" context:nil];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationFinished:)];
    statusWindow.frame = CGRectMake(0.0f, 200.0f, 320.0f, 200.0f); //guessing on y and height values, change them to suit needs 
    [UIView commitAnimations];
    pickerShowing = YES;
}


//ANIMATE out:



-(void)slideOut {
    [UIView beginAnimations:@"slideDown" context:nil];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationFinished:)];
    statusWindow.frame = CGRectMake(0.0f, 480.0f, 320.0f, 200.0f);
    [UIView commitAnimations];
    pickerShowing = NO;
}

-(void)animationFinished:(NSString *)name {
    if ([name isEqualToString:@"slideDown"]) {
        [statusWindow release];
    }
}

答案 2 :(得分:0)

如果要滑入/滑出选择器视图,可以使用Core Animation。 最简单的片段:

// Slide picker view in
[UIView beginAnimations: @"SlideIn" context: nil];
myPickerView.frame = upFrame;
[UIView commitAnimations];

// ...

// Slide picker view out
[UIView beginAnimations: @"SlideOut" context: nil];
myPickerView.frame = downFrame;
[UIView commitAnimations];

upFramedownFrame分别是您在屏幕和屏幕上选择器视图的正确位置进行的CGRect。

希望这有帮助。

答案 3 :(得分:0)

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    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;
    }
}

- (void)slideDownDidStop
{
    // the date picker has finished sliding downwards, so remove it
    [self.pickerView removeFromSuperview];
}

- (IBAction)dateAction:(id)sender
{
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    cell.detailTextLabel.text = [self.dateFormatter stringFromDate:self.pickerView.date];
}

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

您可以从Apple下载一个完整的工作示例应用程序来演示这一点。 http://developer.apple.com/library/ios/#samplecode/DateCell/Introduction/Intro.html