最近公司项目筛选话题里有个日期范围选择,考虑到开始时间低于结束时间,就写了个有时间范围的日期选择器,仅供参考:
关于pickerView相关的代码
#pragma mark - --- delegate 视图委托 ---
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 3;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component == 0) {
if (self.beginDate) {
return 2100 - _year;
}
if (self.endDate) {
return _year + 1 - self.yearMin ;
}
return yearSum;
}else if(component == 1) {
if (self.beginDate) {//有起始时间的情况下
NSInteger yearSelected = [pickerView selectedRowInComponent:0] + self.yearMin ;
if (yearSelected == [NSCalendar currentYear:self.beginDate]) {
return 13 - [NSCalendar currentMonth:self.beginDate];
} else {
return 12;
}
}
if (self.endDate) {//有结束时间的情况下
NSInteger yearSelected = [pickerView selectedRowInComponent:0] + self.yearMin ;
if (yearSelected == [NSCalendar currentYear:self.endDate]) {
return [NSCalendar currentMonth:self.endDate];
} else {
return 12;
}
}
return 12;
}else {
if (self.beginDate) {//有起始时间的情况下
NSInteger yearSelected = [pickerView selectedRowInComponent:0] + self.yearMin ;
if (yearSelected == [NSCalendar currentYear:self.beginDate]) {
NSInteger monthSelected = [pickerView selectedRowInComponent:1] + [NSCalendar currentMonth:self.beginDate];
if ([pickerView selectedRowInComponent:1] == 0) {
return [NSCalendar getDaysWithYear:yearSelected month:monthSelected] - [NSCalendar currentDay:self.beginDate] + 1;
} else {
return [NSCalendar getDaysWithYear:yearSelected month:monthSelected];
}
} else {
NSInteger monthSelected = [pickerView selectedRowInComponent:1] + 1;
return [NSCalendar getDaysWithYear:yearSelected month:monthSelected];
}
}
if (self.endDate) {//有结束时间的情况下
NSInteger yearSelected = [pickerView selectedRowInComponent:0] + self.yearMin ;
if (yearSelected == [NSCalendar currentYear:self.endDate]) {
NSInteger monthSelected = [pickerView selectedRowInComponent:1] + 1;
if (monthSelected == [NSCalendar currentMonth:self.endDate]) {
return [NSCalendar currentDay:self.endDate];
} else {
return [NSCalendar getDaysWithYear:yearSelected month:monthSelected];
}
} else {
NSInteger monthSelected = [pickerView selectedRowInComponent:1] + 1;
return [NSCalendar getDaysWithYear:yearSelected month:monthSelected];
}
}
NSInteger yearSelected = [pickerView selectedRowInComponent:0] + self.yearMin ;
NSInteger monthSelected = [pickerView selectedRowInComponent:1] + 1;
return [NSCalendar getDaysWithYear:yearSelected month:monthSelected];
}
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return PickerViewLabelWeight;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
switch (component) {
case 0:
[pickerView reloadComponent:1];
[pickerView reloadComponent:2];
[pickerView selectRow:(12 - [NSCalendar currentMonth:self.beginDate]) inComponent:1 animated:NO];
[pickerView selectRow:0 inComponent:1 animated:NO];
[pickerView selectRow:(28 - [NSCalendar currentDay:self.beginDate]) inComponent:2 animated:NO];
[pickerView selectRow:0 inComponent:2 animated:NO];
self.year = [self.pickerView selectedRowInComponent:0] + self.yearMin;
if (self.beginDate) {
if (self.year == self.yearMin) {
self.month = [NSCalendar currentMonth:self.beginDate];
self.day = [NSCalendar currentDay:self.beginDate];
} else {
self.month = 1;
self.day = 1;
}
} else {
self.month = 1;
self.day = 1;
}
break;
case 1:
[pickerView reloadComponent:2];
[pickerView selectRow:(28 - [NSCalendar currentDay:self.beginDate]) inComponent:2 animated:NO];
[pickerView selectRow:0 inComponent:2 animated:NO];
if (self.beginDate) {
if (self.year == self.yearMin) {
self.month = [self.pickerView selectedRowInComponent:1] + [NSCalendar currentMonth:self.beginDate];
if ([self.pickerView selectedRowInComponent:1] == 0) {
self.day = [NSCalendar currentDay:self.beginDate];
} else {
self.day = 1;
}
} else {
self.month = [self.pickerView selectedRowInComponent:1] + 1;
self.day = 1;
}
} else {
self.month = [self.pickerView selectedRowInComponent:1] + 1;
self.day = 1;
}
default:
if (self.beginDate) {
if (self.year == self.yearMin) {
if (self.month == [NSCalendar currentMonth:self.beginDate]) {
self.day = [self.pickerView selectedRowInComponent:2] + [NSCalendar currentDay:self.beginDate];
} else {
self.day = [self.pickerView selectedRowInComponent:2] + 1;
}
} else {
self.day = [self.pickerView selectedRowInComponent:2] + 1;
}
}else {
self.day = [self.pickerView selectedRowInComponent:2] + 1;
}
break;
}
[self reloadData];
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view
{
NSString *text;
if (component == 0) {
if (self.beginDate) {
text = [NSString stringWithFormat:@"%zd", row + [NSCalendar currentYear:self.beginDate]];
} else {
text = [NSString stringWithFormat:@"%zd", row + self.yearMin ];
}
}else if (component == 1){
if (self.beginDate) {
if (self.year == self.yearMin ) {
text = [NSString stringWithFormat:@"%zd", row + [NSCalendar currentMonth:self.beginDate]];
} else {
text = [NSString stringWithFormat:@"%zd", row + 1];
}
} else {
text = [NSString stringWithFormat:@"%zd", row + 1];
}
}else{
if (self.beginDate) {
if (self.year == self.yearMin ) {
if (self.month == [NSCalendar currentMonth:self.beginDate]) {
text = [NSString stringWithFormat:@"%zd", row + [NSCalendar currentDay:self.beginDate]];
} else {
text = [NSString stringWithFormat:@"%zd", row + 1];
}
} else {
text = [NSString stringWithFormat:@"%zd", row + 1];
}
} else {
text = [NSString stringWithFormat:@"%zd", row + 1];
}
}
UILabel *label = [[UILabel alloc]init];
[label setTextAlignment:NSTextAlignmentCenter];
[label setFont:[UIFont systemFontOfSize:17]];
[label setText:text];
return label;
}