在UITableViewCell选择后,UIDatePicker不会显示

时间:2011-09-08 07:38:23

标签: iphone objective-c ios cocoa-touch ipad

我使用Apple的Code示例,在选择特定的UITableViewCell时显示UIDatePicker。

我该如何改变这种行为?

Messed up view

我的代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

[tableView deselectRowAtIndexPath:indexPath animated:YES];

if(indexPath.section == 5) {
    //Date Picker
    UITableViewCell *targetCell = [tableView cellForRowAtIndexPath:indexPath];
    self.pickerView.date = [self.dateFormatter dateFromString:targetCell.textLabel.text];
    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];

    }

}


}

2 个答案:

答案 0 :(得分:0)

我从给定图片中看到的是,您试图将UIPickerView从屏幕底部向上滑动。

如果是这样,那么我认为你不是应该设置框架。

View and Window Architecture所示,坐标系的原点是左上角。

我应该这样做:

CGRect pickerFrame = pickerView.frame;
pickerFrame.origin.x = leftTableViewWidth;
pickerFrame.origin.y = screenFrame.size.height + pickerFrame.size.height;
pickerView.frame = pickerFrame;
// begin animation here
pickerFrame.origin.y += pickerFrame.size.height;
// commit animation

答案 1 :(得分:0)

假设您提供的代码属于detail ViewController,则无需为pickerRect设置框架。只需告诉应用程序,当方向发生变化时,pickerView应随之旋转,如下所示

self.picker.autoresizingMask = UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleTopMargin;

您可以通过设置框架来强制选择器视图的大小,就像您所做的那样。由于您正在使用UISplitViewController,因此pickerView必须是detail ViewController的子视图,而不是self.view.window。另外,请查看this link提供的示例,了解您正在犯的其他错误。