删除/隐藏UIDatePicker最小/最大日期范围之外的行?

时间:2021-01-15 16:01:25

I have a UIDatePicker with set minimum and maximum dates. I'm wondering if there is a way to hide the rows of the columns for the dates/times that are either before my minimum date or after my maximum date. Right now the picker displays every single day but only the current week is available to select (bold), what I would like is to have the numbers outside the range of the given week to be hidden from view. can this be done with the UIDatePicker provided by XCode or would I have to build my own picker from scratch?

我有一个UIDatePicker,设置了最小和最大日期。我想知道是否有办法隐藏日期/时间列的行,这些日期/时间是在我的最小日期之前或在我的最大日期之后。现在选择器显示每一天,但只有当前周可供选择(粗体),我想要的是将给定周范围之外的数字隐藏起来。这可以通过XCode提供的UIDatePicker完成,还是我必须从头开始构建自己的选择器?

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.formatter = [NSDateFormatter new];
    [self.formatter setDateFormat:@"dd:hh:mm:ss"];

    NSDate *now = [NSDate date];

    picker.minimumDate = [NSDate date];
    picker.maximumDate = [now dateByAddingTimeInterval:604800];

    [picker setDate:now animated:YES];
    self.counterLabel.text = [now description];

    self.now = [NSDate date];
    self.counterLabel.text = [self.formatter stringFromDate:self.now];

}

1 个解决方案

#1


2  

Using the minimumDate and maximumDate APIs will behave as you've explained - still show the dates but not allow selecting them. There is currently no way to hide those dates that are out of the provided range, however I do have a solution for you.

使用minimumDate和maximumDate API将按照您的说明运行 - 仍然显示日期但不允许选择它们。目前无法隐藏超出所提供范围的日期,但我确实为您提供了解决方案。

Instead of using the min and max dates with a UIDatePicker, you can generate an array of all of the NSDates you want to show to the user, and use a UIPickerView to present them to the user. I've done exactly this for one of my own apps.

您可以生成要向用户显示的所有NSDate的数组,而不是将最小和最大日期与UIDatePicker一起使用,并使用UIPickerView将它们呈现给用户。我已经为我自己的一个应用做了这个。

self.datePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 162)];
self.datePicker.dataSource = self;
self.datePicker.delegate = self;
//...

#pragma mark - UIPickerView Data Source and Delegate

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return [self.availableDates count];
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 28;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    label.font = [UIFont systemFontOfSize:20];
    label.textColor = [UIColor blackColor];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"EEE, MMMM d";

    label.text = [dateFormatter stringFromDate:self.availableDates[row]];

    [label sizeToFit];

    return label;
}

#1


2  

Using the minimumDate and maximumDate APIs will behave as you've explained - still show the dates but not allow selecting them. There is currently no way to hide those dates that are out of the provided range, however I do have a solution for you.

使用minimumDate和maximumDate API将按照您的说明运行 - 仍然显示日期但不允许选择它们。目前无法隐藏超出所提供范围的日期,但我确实为您提供了解决方案。

Instead of using the min and max dates with a UIDatePicker, you can generate an array of all of the NSDates you want to show to the user, and use a UIPickerView to present them to the user. I've done exactly this for one of my own apps.

您可以生成要向用户显示的所有NSDate的数组,而不是将最小和最大日期与UIDatePicker一起使用,并使用UIPickerView将它们呈现给用户。我已经为我自己的一个应用做了这个。

self.datePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 162)];
self.datePicker.dataSource = self;
self.datePicker.delegate = self;
//...

#pragma mark - UIPickerView Data Source and Delegate

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return [self.availableDates count];
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 28;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    label.font = [UIFont systemFontOfSize:20];
    label.textColor = [UIColor blackColor];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"EEE, MMMM d";

    label.text = [dateFormatter stringFromDate:self.availableDates[row]];

    [label sizeToFit];

    return label;
}