当前位置: 首页>编程语言>正文

自定义UIDatePickerView

因为项目中需要使用到时间选择器,需求是最大只能选择当天,超过当前天的日期不显示,系统的无法满足需求,如是自己定义了一个时间选择器,废话不多说,效果如下:
自定义UIDatePickerView,第1张
IMG_0894.GIF

.h文件

@interface P_TDatePickerView : UIPickerView

@property (nonatomic ,assign)NSInteger year;///当前年

@property (nonatomic ,assign)NSInteger month;///当前月

@property (nonatomic ,assign)NSInteger day;///当前日

@property (nonatomic ,assign)NSInteger monthRow;

@property (nonatomic ,assign)NSInteger dayRow;

@property (nonatomic ,strong)NSArray * yearArray;///年份数组

@property (nonatomic ,strong)NSArray * monthArray;///月份数组

@property (nonatomic ,strong)NSArray * dateArray;///日期数组

@property (nonatomic, assign) NSInteger state;///1、从中间某个时间段展示

@property (nonatomic, strong) NSString * date;///需要展示的年月e日

///选中的行,手动滑动

-(void)P_TDatePickerView:(P_TDatePickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;

- (UIView*)P_TDatePickerView:(P_TDatePickerView*)pickerViewviewForRow:(NSInteger)rowforComponent:(NSInteger)componentreusingView:(UIView*)view;

///刷新数据

- (void)P_TReloadAllData;

@end

.m文件

#import "P_TDatePickerView.h"

@interface P_TDatePickerView()

@end

@implementation P_TDatePickerView

- (void)awakeFromNib{
    [super awakeFromNib];

    if (self) {
        //获取当前年月日
        NSDate * date = [NSDate date];
        NSDateFormatter * matter = [[NSDateFormatter alloc] init];
        [matter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
        NSString *dateStr;
        dateStr=[matter stringFromDate:date];
        NSArray * array = [dateStr componentsSeparatedByString:@" "];
        if (array.count == 2) {
            NSString * string = array[0];
            NSArray * dateArray = [string componentsSeparatedByString:@"-"];
            if (dateArray.count == 3) {
                self.year = [dateArray[0] integerValue];
                self.month = [dateArray[1] integerValue];
                self.day = [dateArray[2] integerValue];
            }
        }
        NSMutableArray * dateArray = [[NSMutableArray alloc] init];
        for (int i = 1; i<32; i++) {
            if (i<10) {
                [dateArray addObject:[NSString stringWithFormat:@"0%d",i]];
            }else{
                [dateArray addObject:[NSString stringWithFormat:@"%d",i]];
            }
        }
        self.dateArray = dateArray;
        self.dayRow = self.day;
//        self.dayRow = 31;//从最小年份开始
        NSMutableArray * monthArray = [[NSMutableArray alloc] init];
        for (int i = 1; i<13; i++) {
            if (i<10) {
                [monthArray addObject:[NSString stringWithFormat:@"0%d",i]];
            }else{
                [monthArray addObject:[NSString stringWithFormat:@"%d",i]];
            }
        }
        self.monthArray = monthArray;
        self.monthRow = self.month;//从最新年份开始
//        self.monthRow = 12;//从最小年份开始
    }
}
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        //获取当前年月日
        NSDate * date = [NSDate date];
        NSDateFormatter * matter = [[NSDateFormatter alloc] init];
        [matter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
        NSString *dateStr;
        dateStr=[matter stringFromDate:date];
        NSArray * array = [dateStr componentsSeparatedByString:@" "];
        if (array.count == 2) {
            NSString * string = array[0];
            NSArray * dateArray = [string componentsSeparatedByString:@"-"];
            if (dateArray.count == 3) {
                self.year = [dateArray[0] integerValue];
                self.month = [dateArray[1] integerValue];
                self.day = [dateArray[2] integerValue];
            }
        }
        NSMutableArray * yearArray = [[NSMutableArray alloc] init];
        for (int i = (int)self.year -99; i <= self.year; i++) {
            [yearArray addObject:[NSString stringWithFormat:@"%d",i]];
        }
        self.yearArray = yearArray;
        
        NSMutableArray * dateArray = [[NSMutableArray alloc] init];
        for (int i = 1; i<32; i++) {
            if (i<10) {
                [dateArray addObject:[NSString stringWithFormat:@"0%d",i]];
            }else{
                [dateArray addObject:[NSString stringWithFormat:@"%d",i]];
            }
        }
        self.dateArray = dateArray;
        self.dayRow = 31;
        NSMutableArray * monthArray = [[NSMutableArray alloc] init];
        for (int i = 1; i<13; i++) {
            if (i<10) {
                [monthArray addObject:[NSString stringWithFormat:@"0%d",i]];
            }else{
                [monthArray addObject:[NSString stringWithFormat:@"%d",i]];
            }
        }
        self.monthArray = monthArray;
        self.monthRow = 12;
    }
    UIView * line = [[UIView alloc] initWithFrame:CGRectMake(0, 39, frame.size.width, 1.0)];
    line.backgroundColor = MainColor;
    [self addSubview:line];
    UIView * line1 = [[UIView alloc] initWithFrame:CGRectMake(0, 80, frame.size.width, 1.0)];
    line1.backgroundColor = MainColor;
    [self addSubview:line1];
    return self;
}

#pragma mark ===============刷新数据,确认当前每列返回的行数===============
- (void)P_TReloadAllData{
    if ([self.yearArray[[self selectedRowInComponent:0]] integerValue] == self.year) {//年份相同
        self.monthRow = self.month;//从最新年份开始
        if ([self.monthArray[[self selectedRowInComponent:1]] integerValue] == self.month) {
            self.dayRow = self.day;
        }else{
            self.dayRow = [P_TDatePickerView returnDayRowWithSKDatePickerView:[self.monthArray[[self selectedRowInComponent:1]] integerValue] currentDay:31 isLeapYear:[self isLeapYear:[self.yearArray[[self selectedRowInComponent:0]] integerValue]]];
        }
    }else{
        self.monthRow = 12;
        self.dayRow = [P_TDatePickerView returnDayRowWithSKDatePickerView:[self.monthArray[[self selectedRowInComponent:1]] integerValue] currentDay:31 isLeapYear:[self isLeapYear:[self.yearArray[[self selectedRowInComponent:0]] integerValue]]];
    }
    [self reloadAllComponents];
}

#pragma mark ===============返回样式===============
- (UIView *)P_TDatePickerView:(P_TDatePickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    CGFloat pkViewWidth = pickerView.frame.size.width;
    if (!view){
        view = [[UIView alloc] init];
    }
    UILabel *text = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, pkViewWidth/3.0, 40)];
    text.textAlignment = NSTextAlignmentCenter;
    switch (component) {
        case 0:{
            text.text = [NSString stringWithFormat:@"%@",pickerView.yearArray[row]];
        }
            break;
        case 1:{
            text.text = [NSString stringWithFormat:@"%@",pickerView.monthArray[row]];
        }
            break;
        default:{
            text.text = [NSString stringWithFormat:@"%@",pickerView.dateArray[row]];
        }
            break;
    }
    text.textAlignment = NSTextAlignmentCenter;
    [view addSubview:text];
    return view;
}
//选中的行,手动滑动
-(void)P_TDatePickerView:(P_TDatePickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    P_TDatePickerView * dpickerView = (P_TDatePickerView *)pickerView;
    switch (component) {
        case 0:{
            if ([dpickerView.yearArray[row] integerValue] == dpickerView.year) {
                //判断是否是当前年
                //当前年,刷新月份,如果选中的月份大于当前月份,则取当前月份,反之不滑动
                dpickerView.monthRow = dpickerView.month;//限定月份返回的行数
                [pickerView reloadComponent:1];
                if ([dpickerView selectedRowInComponent:1] >= dpickerView.month-1) {
                    dpickerView.dayRow = [P_TDatePickerView returnDayRowWithSKDatePickerView:dpickerView.month currentDay:self.day isLeapYear:[dpickerView isLeapYear:[dpickerView.yearArray[row] integerValue]]];
                }else{
                    dpickerView.dayRow = [P_TDatePickerView returnDayRowWithSKDatePickerView:[dpickerView.monthArray[[dpickerView selectedRowInComponent:1]] integerValue] currentDay:31 isLeapYear:[dpickerView isLeapYear:[dpickerView.yearArray[row] integerValue]]];
                }
                [pickerView reloadComponent:2];
                
            }else{
                dpickerView.dayRow = [P_TDatePickerView returnDayRowWithSKDatePickerView:[dpickerView.monthArray[[dpickerView selectedRowInComponent:1]] integerValue] currentDay:31 isLeapYear:[dpickerView isLeapYear:[dpickerView.yearArray[row] integerValue]]];
                dpickerView.monthRow = 12;
                [pickerView reloadComponent:1];
                [pickerView reloadComponent:2];
            }
        }
            break;
        case 1:{
            NSInteger dayrow;
            if ([dpickerView.yearArray[[dpickerView selectedRowInComponent:0]] integerValue]== dpickerView.year) {
                //判断是否当前年份
                //当前月,刷新日期,如果选中的日期大于当前日期,则取当前日期,反之不滑动
                if ([dpickerView.monthArray[row] integerValue] == dpickerView.month) {
                    //判断选中月份天数
                    dayrow = [P_TDatePickerView returnDayRowWithSKDatePickerView:dpickerView.month currentDay:self.day isLeapYear:[dpickerView isLeapYear:[dpickerView.yearArray[[dpickerView selectedRowInComponent:0]] integerValue]]];
                }else{
                    //判断选中月份天数
                    dayrow = [P_TDatePickerView returnDayRowWithSKDatePickerView:[dpickerView.monthArray[row] integerValue] currentDay:31 isLeapYear:[dpickerView isLeapYear:[dpickerView.yearArray[[dpickerView selectedRowInComponent:0]] integerValue]]];
                }
            }else{
                //判断选中月份天数
                dayrow = [P_TDatePickerView returnDayRowWithSKDatePickerView:[dpickerView.monthArray[row] integerValue] currentDay:31 isLeapYear:[dpickerView isLeapYear:[dpickerView.yearArray[[dpickerView selectedRowInComponent:0]] integerValue]]];
            }
            dpickerView.dayRow = dayrow;
            [pickerView reloadComponent:2];
        }
            break;
        default:
            break;
    }
}
#pragma mark ===============剥离判断日期返回行数的方法===============
/**
 返回天数的行数
 @param month 判断月份
 @param currentDay 对比的天数
 @param isleapYear 是否闰年
 @return 返回天数
 */
+ (NSInteger)returnDayRowWithSKDatePickerView:(NSInteger )month currentDay:(NSInteger)currentDay isLeapYear:(BOOL)isleapYear{
    NSInteger dayrow;
    //判断月份
    if (month == 2) {
        if (isleapYear) {
            dayrow = MIN(currentDay, 29);
        }else{
            dayrow = MIN(currentDay, 28);
        }
    }else if(month == 4||month == 6||month == 9||month == 11){
        dayrow = MIN(currentDay, 30);
    }else{
        dayrow = currentDay;
    }
    return dayrow;
}
#pragma mark ===============是否是闰年===============
- (BOOL)isLeapYear:(NSInteger)year{
    if(year%400 == 0||(year %100 != 0&&year%4 == 0)){
        return YES;
    }else{
        return NO;
    }
}
@end

可以xib 也可以代码布局,布局就不多说
主要是实现代理方法,代码如下:

#pragma mark ===============pickerViewDelegate===============
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 3;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    P_TDatePickerView * dpickView = (P_TDatePickerView *)pickerView;
    NSInteger row;
    switch (component) {
        case 0:
            row = [dpickView.yearArray count];
            break;
        case 1:
            row = dpickView.monthRow;
            break;
        case 2:
            row = dpickView.dayRow;
            break;
        default:
            return 0;
            break;
    }
    return row;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 40;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    P_TDatePickerView * dpickView = (P_TDatePickerView *)pickerView;
    return [dpickView P_TDatePickerView:dpickView viewForRow:row forComponent:component reusingView:view];
}

详细代码传送门
http://code.cocoachina.com/view/1000458

如果正好可以解决您的需求,麻烦动动你的小手,给个Star。
如有疑问,可联系qq734651142


https://www.xamrdz.com/lan/5bp2016658.html

相关文章: