最近公司項目篩選話題里有個日期范圍選擇,考慮到開始時間低于結(jié)束時間,就寫了個有時間范圍的日期選擇器,僅供參考:
關(guān)于pickerView相關(guān)的代碼
#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) {//有結(jié)束時間的情況下
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) {//有結(jié)束時間的情況下
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;
}