如何在iOS中的表格单元格中显示日期选择器和选择器视图?

时间:2014-03-29 06:34:28

标签: iphone objective-c ios7

我见过这个        https://developer.apple.com/library/ios/samplecode/DateCell/Introduction/Intro.html(日期单元格)我的问题,但这是在这个例子中使用存储板,我不使用商店板。那么如何在我的视图控制器中执行此操作。

1 个答案:

答案 0 :(得分:0)

使用此:

// Cell.m

@interface Cell ()

@property (strong, nonatomic) UIPickerView *pickerView;

@end    

@implementation

- (void)initPickerView {
    _pickerView = [[UIPickerView alloc] init];
}

- (void)showPickerView {
    CGRect pickerViewFrame = _pickerView.frame;
    pickerViewFrame.origin.y = self.frame.size.height;
    _pickerView.frame = pickerViewFrame;

    [self addSubview:_pickerView];

    CGRect cellFrame = self.frame;
    cellFrame.size.height += pickerViewFrame.size.height;
    self.frame = cellFrame;
}

- (void)hidePickerView {
    CGRect cellFrame = self.frame;
    cellFrame.size.height -= _pickerView.frame.size.height;
    self.frame = cellFrame;

    [_pickerView removeFromSuperview];
}

@end

首先,在使用视图之前,使用initPickerView方法在任何位置初始化选择器视图。然后,当您需要选择器视图时,请使用showPickerView。并使用hidePickerView将其从单元格中删除。