删除/隐藏UIDatePicker的最小/最大日期范围之外的行? [英] remove/hide rows outside minimum/maximum date range of UIDatePicker?

查看:129
本文介绍了删除/隐藏UIDatePicker的最小/最大日期范围之外的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIDatePicker,其中设置了最小和最大日期.我想知道是否有一种方法可以隐藏最短日期之前或最长日期之后的日期/时间的列的行.现在,选择器每天显示一次,但只有当前星期可供选择(粗体),我想将超出给定星期范围的数字隐藏起来.可以使用XCode提供的UIDatePicker完成此操作,还是必须从头开始构建自己的选择器?

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?

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

}

推荐答案

使用minimumDatemaximumDate API的行为符合您的解释-仍显示日期,但不允许选择它们.当前无法隐藏那些超出提供范围的日期,但是我确实为您提供了一种解决方案.

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.

您可以生成要显示给用户的所有NSDate的数组,而不是使用UIDatePicker的最小日期和最大日期,并使用UIPickerView将其显示给用户.我已经为自己的一个应用程序完成了此操作.

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.

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

这篇关于删除/隐藏UIDatePicker的最小/最大日期范围之外的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆