概述
自定义时间选择器,难点在于时间数据的获取,主要是在时间进位的时候,数据可能会大范围的改变,列如:
在59分时,进位后下一分钟是0分,此时小时值会加1,如果上一分钟小时数为23,则小时数据进位后变成第二天的0点,如果上一分钟天数是月底最后一天,则进位后月份也会跟着进位加1,如果上一分钟月份数为12,则进位后月份数会变成1月,此时年份数加1。
另外一个注意事项是,时间是时时刻变化的,所以要保证时间选择器上的数据正确有效,需要让它过一段时间间隔就刷新重新获取数据,然后刷新控件
界面效果图
代码
数据获取的关键代码
-(void)getData{
/*
获取时间数据分为两种情况,一种是常规数据,如一个月28/29/30/31天,一天24小时,一小时60分钟等,这个数据很好获取
另一种情况是当前的数据,当前时间数据比较复杂,前提条件:分钟只需要取10钟一间隔得数据即:0、10、20、30、40、50,这样获取数据的时候需要在当前时间加10分钟,这样的话 在50~59分,小时数据是没有当前的小时值可选的;在23时50~59分,当前天数是不可选的;在每月的最后一天和每年的最后一天都有类似的特殊情况,同时需要每个一分钟更新一次当前的时间的数据
*/
//在当前时间加10分钟,然后获取年月日时分的值
NSDate * date = [NSDate dateWithTimeIntervalSinceNow:60*10];//提前10分钟
NSCalendar * canlendar = [NSCalendar currentCalendar];
NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDateComponents * components = [canlendar components:unitFlags fromDate:date];
NSInteger nowY = [components year];
NSInteger nowM = [components month];
NSInteger nowD = [components day];
NSInteger nowH = [components hour];
NSInteger nowF = [components minute];
//给当前值赋值--这些值主要是用于时间选择器的第一次打开时显示当前时间
self.defaultYear = [components year];
self.defaultMonth = [components month];
self.defaultDay = [components day];
self.defaultHour = [components hour];
NSLog(@"%ld",[components minute]);
self.defaultMinute = ([components minute]/10)*10 ;
//获取当前月的天数
NSRange range = [canlendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];
NSUInteger numberOfDaysInMonth = range.length;
NSMutableArray * nowPickerViewDataArr0 = [[NSMutableArray alloc] init];
NSMutableArray * nowPickerViewDataArr1 = [[NSMutableArray alloc] init];
NSMutableArray * nowPickerViewDataArr2 = [[NSMutableArray alloc] init];
NSMutableArray * nowPickerViewDataArr3 = [[NSMutableArray alloc] init];
NSMutableArray * nowPickerViewDataArr4 = [[NSMutableArray alloc] init];
//获取当前分钟数
if (nowF >= 50) {//大于50分到59之间分钟数需要特殊处理
self.defaultMinute = 0;
self.defaultHour+=1;
if (self.defaultHour == 24) {
self.defaultHour = 0;
}
for (int i = 0; i < 6; i++) {
[nowPickerViewDataArr4 addObject:[NSNumber numberWithInteger:i*10]];
}
}else{//小于50分,则只获取当前可选的分钟值
for (NSInteger i = self.defaultMinute/10; i < 6; i++) {
[nowPickerViewDataArr4 addObject:[NSNumber numberWithInteger:i*10]];
}
}
//获取当前小时数
if (nowH >= 23&&nowH>=50) {//23点50分到59之间,当前小时数需要特殊处理
self.defaultHour+=1;
self.defaultDay+=1;
if (self.defaultDay >numberOfDaysInMonth) {
self.defaultDay = 1;
}
for (int i = 0; i < 24; i++) {
[nowPickerViewDataArr3 addObject:[NSNumber numberWithInteger:i]];
}
}else{//
for (NSInteger i = self.defaultHour; i < 24; i++) {
[nowPickerViewDataArr3 addObject:[NSNumber numberWithInteger:i]];
}
}
//获取当前日数
if (nowD >= numberOfDaysInMonth&&nowH >= 23&&nowH>=50) {//每月最后一天23点50分到59之间,当前小时数需要特殊处理
self.defaultDay+=1;
self.defaultMonth+=1;
if (self.defaultMonth >12) {
self.defaultMonth = 1;
}
for (int i = 1; i <= numberOfDaysInMonth; i++) {
[nowPickerViewDataArr2 addObject:[NSNumber numberWithInteger:i]];
}
}else{//
for (NSInteger i = self.defaultDay; i <= numberOfDaysInMonth; i++) {
[nowPickerViewDataArr2 addObject:[NSNumber numberWithInteger:i]];
}
}
//获取当前月份数
if (nowM >= 12&&nowD >= numberOfDaysInMonth&&nowH >= 23&&nowH>=50) {//每年12月最后一天23点50分到59之间,当前小时数需要特殊处理
self.defaultMonth+=1;
self.defaultYear+=1;
if (self.defaultYear >nowY) {
self.defaultYear = nowY+1;
}
for (int i = 1; i <= 12; i++) {
[nowPickerViewDataArr1 addObject:[NSNumber numberWithInteger:i]];
}
}else{//
for (NSInteger i = self.defaultMonth; i <= 12; i++) {
[nowPickerViewDataArr1 addObject:[NSNumber numberWithInteger:i]];
}
}
//获取年份
for (int i = 0; i < 3; i++) {
[nowPickerViewDataArr0 addObject:[NSNumber numberWithInteger:i+self.defaultYear]];
}
//将当前时间的数据存到数组
[self.nowPickerViewDataArr addObject:nowPickerViewDataArr0];
[self.nowPickerViewDataArr addObject:nowPickerViewDataArr1];
[self.nowPickerViewDataArr addObject:nowPickerViewDataArr2];
[self.nowPickerViewDataArr addObject:nowPickerViewDataArr3];
[self.nowPickerViewDataArr addObject:nowPickerViewDataArr4];
//获取常规数值
NSMutableArray * pickerViewDataArr1 = [[NSMutableArray alloc] init];
NSMutableArray * pickerViewDataArr2 = [[NSMutableArray alloc] init];
NSMutableArray * pickerViewDataArr3 = [[NSMutableArray alloc] init];
NSMutableArray * pickerViewDataArr4 = [[NSMutableArray alloc] init];
//分钟
for (int i = 0; i < 6; i++) {
[pickerViewDataArr4 addObject:[NSNumber numberWithInteger:i*10]];
}
//小时
for (NSInteger i = 0; i < 24; i++) {
[pickerViewDataArr3 addObject:[NSNumber numberWithInteger:i]];
}
//天数
for (NSInteger i = 1; i <= numberOfDaysInMonth; i++) {
[pickerViewDataArr2 addObject:[NSNumber numberWithInteger:i]];
}
//月数
for (NSInteger i = 1; i <= 12; i++) {
[pickerViewDataArr1 addObject:[NSNumber numberWithInteger:i]];
}
[self.pickerViewDataArr addObject:nowPickerViewDataArr0];
[self.pickerViewDataArr addObject:pickerViewDataArr1];
[self.pickerViewDataArr addObject:pickerViewDataArr2];
[self.pickerViewDataArr addObject:pickerViewDataArr3];
[self.pickerViewDataArr addObject:pickerViewDataArr4];
}
pickerView返回行数的代理方法关键代码
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
if (component == 0) {
return [self.nowPickerViewDataArr[0] count];
} else if(component == 1){
if (self.selectRow0 == 0) {
return [self.nowPickerViewDataArr[1] count];
} else {
return [self.pickerViewDataArr[1] count];
}
}else if (component ==2){
if (self.selectRow0 == 0&&self.selectRow1 == 0) {
return [self.nowPickerViewDataArr[2] count];
} else {
return [self.pickerViewDataArr[2] count];
}
}else if (component == 3){
if (self.selectRow2 == 0&&self.selectRow1 == 0&&self.selectRow0 == 0) {
return [self.nowPickerViewDataArr[3] count];
} else {
return [self.pickerViewDataArr[3] count];
}
}else{
if (self.selectRow3 ==0&&self.selectRow2 == 0&&self.selectRow1 == 0&&self.selectRow0 == 0) {
return [self.nowPickerViewDataArr[4] count];
} else {
return [self.pickerViewDataArr[4] count];
}
}
}
pickerView返回显示值的代理方法关键代码
-(UIView*)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
//添加一个label
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth/5, 30)];
label.textColor = [UIColor grayColor];
label.font = [UIFont systemFontOfSize:15];
label.textAlignment = NSTextAlignmentLeft;
//对每一列都需要做特殊和非特殊的判断,一遍给到正确的值
if (component == 0) {
label.text = [NSString stringWithFormat:@"%@",self.nowPickerViewDataArr[0][row]];
} else if(component == 1){
if (self.selectRow0 == 0) {
label.text = [NSString stringWithFormat:@"%@",self.nowPickerViewDataArr[1][row]];
} else {
label.text = [NSString stringWithFormat:@"%@",self.pickerViewDataArr[1][row]];
}
}else if (component ==2){
if (self.selectRow0 == 0&&self.selectRow1 == 0) {
label.text = [NSString stringWithFormat:@"%@",self.nowPickerViewDataArr[2][row]];
} else {
label.text = [NSString stringWithFormat:@"%@",self.pickerViewDataArr[2][row]];
}
}else if (component == 3){
if (self.selectRow2 == 0&&self.selectRow1 == 0&&self.selectRow0 == 0) {
label.text = [NSString stringWithFormat:@"%@",self.nowPickerViewDataArr[3][row]];
} else {
label.text = [NSString stringWithFormat:@"%@",self.pickerViewDataArr[3][row]];
}
}else{
if (self.selectRow3 ==0&&self.selectRow2 == 0&&self.selectRow1 == 0&&self.selectRow0 == 0) {
label.text = [NSString stringWithFormat:@"%@",self.nowPickerViewDataArr[4][row]];
} else {
label.text = [NSString stringWithFormat:@"%@",self.pickerViewDataArr[4][row]];
}
}
return label;
}
pickerView选择行的代理方法关键代码
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
//获取选中的列数
self.selectRow0 = [pickerView selectedRowInComponent:0];
self.selectRow1 = [pickerView selectedRowInComponent:1];
self.selectRow2 = [pickerView selectedRowInComponent:2];
self.selectRow3 = [pickerView selectedRowInComponent:3];
self.selectRow4 = [pickerView selectedRowInComponent:4];
//先清空之前的数据,在重新获取数据
self.pickerViewDataArr = nil;
self.nowPickerViewDataArr = nil;
[self getData];
//刷新后面的列
if (component == 0) {//选择完成后加载后面的列的数据但是不刷新当前列
[pickerView reloadComponent:1];
[pickerView reloadComponent:2];
[pickerView reloadComponent:3];
[pickerView reloadComponent:4];
} else if(component == 1){
[pickerView reloadComponent:2];
[pickerView reloadComponent:3];
[pickerView reloadComponent:4];
}else if(component == 2){
[pickerView reloadComponent:3];
[pickerView reloadComponent:4];
}else if(component == 3){
[pickerView reloadComponent:4];
}
}
详细代码看Demo