用UIPikerView封装自己的UIDatePicker
来源:互联网 发布:linux同步时间 编辑:程序博客网 时间:2024/06/06 16:57
首先我们为什么要用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; }}
0 0
- 用UIPikerView封装自己的UIDatePicker
- UIPikerView的属性
- UIPikerView视图的创建
- UIPikerView的属性
- UIPikerView的属性
- UIPikerView的属性
- 封装UIDatePicker,日期选择器
- 自己封装用的Struts2BaseAction
- 自己项目用的一个控件,自己封装的
- 自己封装的ajax
- 自己封装的泛型链表
- 封装自己的Framwork
- 封装自己的理解
- 自己封装的ajax
- MapList 自己封装的
- 封装自己的printf
- 自己封装的轮播图
- 封装自己的jquery
- Spring MVC
- 使用BabeLua在cocos2d-x中调试Lua
- 用SharePreference存储数据
- hdu 1003 :Max Sum
- IOS设置菜单界面的设计思路
- 用UIPikerView封装自己的UIDatePicker
- jquery获取元素宽高,浏览器宽高,滚动距离
- Linux TCP队列相关参数的总结
- Xcode 7快捷真机调试
- JavaScript闭包
- C++中Exit()与atexit()函数的使用
- HashMap 实例[leetcode]Longest Consecutive Sequence
- iOS开发中的内存管理相关
- H5新春贺卡之笔记