利用自定义picker构建一个只显示年月的DatePicker。
话不多说,直接上代码:
- (void)createPicker;
{
//构造可选年的数组
[self pickerView];
[self configDateYearRange];
}
- (UIPickerView *)pickerView
{
if (!_pickerView) {
_pickerView = [UIPickerView new];
_pickerView.delegate = (id)self;
_pickerView.dataSource = (id) self;
[self addSubview:_pickerView];
_pickerView.frame = CGRectMake(0, BTNHEIGHT, kScreenWidth, DATEPICKERHEIGHT);
}
return _pickerView;
}
/**
设置datePicker显示的年的取值区间(当前月份-1之后的年)
*/
- (void)configDateYearRange
{
NSDate *max = self.maximumDate;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM"];
NSString *yearMaxStr = [[formatter stringFromDate:max] substringToIndex:4];
NSInteger yearMax = [yearMaxStr integerValue];
NSDate *min = self.minimunDate;
NSString *yearMinStr = [[formatter stringFromDate:min] substringToIndex:4];
NSInteger yearMin = [yearMinStr integerValue];
NSMutableArray *ret = [NSMutableArray array];
for (NSInteger i = yearMin; i <= yearMax; i ++) {
[ret addObject:[NSString stringWithFormat:@"%@",@(i)]];
}
self.yearsArr = ret;
}
/**
把日期字符串转化为picker的数据源
@param dateString 日期字符串
*/
- (void)updateMonthSourceWithDateString:(NSString *)dateString
{
NSString *yearStr = [dateString substringToIndex:4];
NSString *monthStr = nil;
NSDate *max = self.maximumDate;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM"];
NSString *yearMaxStr = [[formatter stringFromDate:max] substringToIndex:4];
NSInteger yearMax = [yearMaxStr integerValue];
BOOL isThisYear = [yearStr integerValue] == yearMax;
if (isThisYear) {
NSDate *max = self.maximumDate;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM"];
monthStr = [[formatter stringFromDate:max] substringFromIndex:5];
}else {
monthStr = @"12";
}
NSMutableArray *months = [NSMutableArray array];
NSInteger month = [monthStr integerValue];
for (NSInteger i = 1; i <= month ; i ++) {
[months addObject:[NSString stringWithFormat:@"%@",@(i)]];
}
self.monthsArr = months;
}
/**
切换年之后要更新月的显示
*/
- (void)updateMonthArr
{
NSMutableArray *months = [NSMutableArray array];
if ([self.pickerView selectedRowInComponent:0] == self.yearsArr.count-1) {
NSDate *max = self.maximumDate;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM"];
NSString *monthStr = [[formatter stringFromDate:max] substringFromIndex:5];
NSInteger month = [monthStr integerValue];
for (NSInteger i = 1; i <= month ; i ++) {
[months addObject:[NSString stringWithFormat:@"%@",@(i)]];
}
self.monthsArr = months;
}else {
for (NSInteger i = 1; i <= 12 ; i ++) {
[months addObject:[NSString stringWithFormat:@"%@",@(i)]];
}
self.monthsArr = months;
}
}
#pragma mark - datasource
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
return 2;
}
// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
if (component == 0) {
return self.yearsArr.count;
}else if (component == 1) {
return self.monthsArr.count;
}
return 0;
}
#pragma mark delegate
// returns width of column and height of row for each component.
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component __TVOS_PROHIBITED;
{
return kScreenWidth/2.f;
}
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component __TVOS_PROHIBITED;
{
if (component == 0) {
return [NSString stringWithFormat:@"%@年",self.yearsArr[row]];;
}else if (component == 1) {
return [NSString stringWithFormat:@"%@月",self.monthsArr[row]];;
}
return @"";
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component __TVOS_PROHIBITED;
{
if (component == 0) {
[self updateMonthArr];
[pickerView reloadComponent:1];
}
}
使用方法也超级简单:
WJDatePicker *pickerView = [[WJDatePicker alloc] initWithType:DatePickerType_YYYYMM];
self.pickerView = pickerView;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM"];
pickerView.minimunDate = [formatter dateFromString:@"2000-01"];
pickerView.maximumDate = [formatter dateFromString:@"2018-05"];
pickerView.timeStr = @"2015-07";
pickerView.clickReturn = ^(NSInteger tag, NSString *timeStr) {
if (0 == tag) { // 取消
NSLog(@"cancel");
} else { // 确认
NSLog(@"%@",timeStr);
}
};
[self.view addSubview:pickerView];
附上下载地址:https://github.com/zgsddzwj/WJDatePicker