首先我们为什么要用UIPickerView封装DatePicker哪?
是因为DatePicker是无法自定制的,只能使用系统提供的哪几种样式,所以我们需要自定制
- 在这里我们只是最简单的封装,如果你还需要一些好看一点的东西,可以自己加
下面上代码
- 这是头文件我们声明的东西
#import "ViewController.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
@interface ViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (nonatomic,strong)UIPickerView * pickerView;
@property (nonatomic,strong)NSCalendar *calendar;
@property (nonatomic,strong)NSDate *startDate; //其实时间
@property (nonatomic,strong)NSDate *endDate; //结束时间
@property (nonatomic,strong)NSDate *selectDate;
@property (nonatomic,strong)NSDateComponents * selectedDateComponets;
@end
- 下面是内容
- (void)viewDidLoad {
[super viewDidLoad];
[self initData];//初始化时间戳
[self initPickerView];//创建pickerView
}
-(void)initData{
_calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
_startDate = [dateFormatter dateFromString:@"1900-01-01"];
_endDate = [NSDate date]; //现在的时间
_selectDate = _startDate;
_selectedDateComponets = [_calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:_selectDate];
}
-(void)initPickerView{
_pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, HEIGHT - 200, WIDTH, 200)];
_pickerView.delegate = self;
_pickerView.dataSource = self;
[self.view addSubview:_pickerView];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 3;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
switch (component) { // component是栏目index,从0开始,后面的row也一样是从0开始
case 0: { // 第一栏为年,这里startDate和endDate为起始时间和截止时间,请自行指定
NSDateComponents *startCpts = [_calendar components:NSCalendarUnitYear fromDate:_startDate];
NSDateComponents *endCpts = [_calendar components:NSCalendarUnitYear fromDate:_endDate];
return [endCpts year] - [startCpts year] + 1;
}
case 1: // 第二栏为月份
return 12;
case 2: { // 第三栏为对应月份的天数
NSRange dayRange = [_calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:self.selectDate];
return dayRange.length;
}
default:
return 0;
}
}
// 每列宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
return WIDTH/3;
}
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
switch (component) {
case 0: {
NSDateComponents *components = [self.calendar components:NSCalendarUnitYear fromDate:self.startDate];
NSString * currentYear = [NSString stringWithFormat:@"%ld", [components year] + row];
return currentYear;
break;
}
case 1: { // 返回月份可以用DateFormatter,这样可以支持本地化
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.locale = [NSLocale currentLocale];
NSArray *monthSymbols = [formatter veryShortMonthSymbols];
return [monthSymbols objectAtIndex:row];
break;
}
case 2: {
NSRange dateRange = [self.calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:self.selectDate];
NSString *currentDay = [NSString stringWithFormat:@"%02ld", (row + 1) % (dateRange.length + 1)];
return currentDay;
break;
}
default:
break;
}
return nil;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
switch (component) {
case 0: {
_selectDate = [dateFormatter dateFromString:[NSString stringWithFormat:@"1900-01-01"]];
NSDateComponents *indicatorComponents = [_calendar components:NSCalendarUnitYear fromDate:_startDate];
NSInteger year = [indicatorComponents year] + row;
NSDateComponents *targetComponents = [_calendar components:unitFlags fromDate:self.selectDate];
[targetComponents setYear:year];
self.selectedDateComponets = targetComponents;
_selectDate = [dateFormatter dateFromString:[NSString stringWithFormat:@"%ld-%ld-%ld",targetComponents.year,_selectedDateComponets.month,_selectedDateComponets.day]];
[pickerView selectRow:0 inComponent:1 animated:YES];//刷新
[pickerView selectRow:0 inComponent:2 animated:YES];//刷新
break;
}
case 1: {
_selectDate = [dateFormatter dateFromString:[NSString stringWithFormat:@"%ld-01-01",_selectedDateComponets.year]];
NSDateComponents *targetComponents = [self.calendar components:unitFlags fromDate:self.selectDate];
[targetComponents setMonth:row + 1];
self.selectedDateComponets = targetComponents;
_selectDate = [dateFormatter dateFromString:[NSString stringWithFormat:@"%ld-%ld-%ld",_selectedDateComponets.year,targetComponents.month,_selectedDateComponets.day]];
[pickerView reloadComponent:2];//移除
[pickerView selectRow:0 inComponent:2 animated:YES];//刷新
break;
}
case 2: {
NSDateComponents *targetComponents = [self.calendar components:unitFlags fromDate:self.selectDate];
[targetComponents setDay:row + 1];
self.selectedDateComponets = targetComponents;
_selectDate = [dateFormatter dateFromString:[NSString stringWithFormat:@"%ld-%ld-%ld",_selectedDateComponets.year,targetComponents.month,_selectedDateComponets.day]];
break;
}
default:
break;
}
}