如何在ios中准备好几个月和几年的UIPickerView?

我正在iOS和iOS系统上准备UIPickerView。

以下是我的代码。

viewdidload中

//Array for picker view monthsArray=[[NSMutableArray alloc]initWithObjects:@"Jan",@"Feb",@"Mar",@"Apr",@"May",@"Jun",@"Jul",@"Aug",@"Sep",@"Oct",@"Nov",@"Dec",nil]; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy"]; NSString *yearString = [formatter stringFromDate:[NSDate date]]; yearsArray=[[NSMutableArray alloc]init]; for (int i=0; i<13; i++) { [yearsArray addObject:[NSString stringWithFormat:@"%d",[yearString intValue]+i]]; } myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 320, 200)]; myPickerView.delegate = self; myPickerView.showsSelectionIndicator = YES; [myPickerView selectRow:0 inComponent:0 animated:YES]; [self.view addSubview:myPickerView]; 

Picker查看委托方法:

 // tell the picker how many components it will have - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 2; } // tell the picker how many rows are available for a given component - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { NSInteger rowsInComponent; if (component==0) { rowsInComponent=[monthsArray count]; } else { rowsInComponent=[yearsArray count]; } return rowsInComponent; } - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { NSString * nameInRow; if (component==0) { nameInRow=[monthsArray objectAtIndex:row]; } else if (component==1) { nameInRow=[yearsArray objectAtIndex:row]; } return nameInRow; } // tell the picker the width of each row for a given component - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component { CGFloat componentWidth ; if (component==0) { componentWidth = 100; } else { componentWidth = 100; } return componentWidth; } 

我得到了以下输出:

PickerImage

但在今年1月至10月的月份已经过期。 如何禁用这些年在我的select器只dynamic当年。 这几个月应该可以用于剩下的几年。

其实真正的产出是,

在这里输入图像说明

在上面,应该在UI中禁用当年的过期月份。

任何意见或build议,将不胜感激。

先谢谢你。

最后,我写了我的逻辑来实现它。

但是可能有更好的逻辑来优化内存。

以下是我的代码。

在viewdidload中:

  currentDateComponents = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]]; //Array for picker view monthsArray=[[NSMutableArray alloc]initWithObjects:@"Jan",@"Feb",@"Mar",@"Apr",@"May",@"Jun",@"Jul",@"Aug",@"Sep",@"Oct",@"Nov",@"Dec",nil]; yearsArray=[[NSMutableArray alloc]init]; for (int i=0; i<13; i++) { [yearsArray addObject:[NSString stringWithFormat:@"%d",[yearString intValue]+i]]; } myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 320, 200)]; myPickerView.delegate = self; myPickerView.showsSelectionIndicator = YES; [myPickerView selectRow:[currentDateComponents month]-1 inComponent:0 animated:YES]; [self.view addSubview:myPickerView]; 

Picker查看委托方法:

 // tell the picker how many components it will have - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 2; } // tell the picker how many rows are available for a given component - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { NSInteger rowsInComponent; if (component==0) { rowsInComponent=[monthsArray count]; } else { rowsInComponent=[yearsArray count]; } return rowsInComponent; } - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { NSLog(@"[currentDateComponents month]-->%d<--",[currentDateComponents month]); NSLog(@"-->%d<--",row); NSLog(@"row->%@<--",[yearsArray objectAtIndex:row]); NSLog(@"-->%@<--",[yearsArray objectAtIndex:[pickerView selectedRowInComponent:1]]); if ([pickerView selectedRowInComponent:0]+1<[currentDateComponents month] && [[yearsArray objectAtIndex:[pickerView selectedRowInComponent:1]] intValue]==[currentDateComponents year]) { [pickerView selectRow:[currentDateComponents month]-1 inComponent:0 animated:YES]; NSLog(@"Need to shift"); } if (component==1) { [pickerView reloadComponent:0]; } } - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 44)]; label.backgroundColor = [UIColor clearColor]; label.textColor = [UIColor blackColor]; label.font = [UIFont fontWithName:@"Arial-BoldMT" size:17]; label.text = component==0?[monthsArray objectAtIndex:row]:[yearsArray objectAtIndex:row]; if (component==0) { if (row+1<[currentDateComponents month] && [[yearsArray objectAtIndex:[pickerView selectedRowInComponent:1]] intValue]==[currentDateComponents year]) { label.textColor = [UIColor grayColor]; } } return label; } // tell the picker the width of each row for a given component - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component { CGFloat componentWidth ; if (component==0) { componentWidth = 100; } else { componentWidth = 100; } return componentWidth; } 

这是我的输出

2013年:

在这里输入图像说明

其他年份:

在这里输入图像说明

感谢您的贡献。

如果你的问题提到date and year所以它看起来像UIDatePickerModeDate就足够了。

你正在寻找month and year ,这不是一个可用的选项。 我build议你考虑使用一个双组件的UIPickerView对象。

原始答复

您可以通过在右侧栏(Cmd + Option + 4)中的“属性”检查器中将“ Date Picker下的“ Mode property更改为“ Date Picker来完成此操作。 你也可以通过编程来做到这一点,

  datePicker.datePickerMode = UIDatePickerModeDate; 

UIDatePickerModeDate

 The date picker displays months, days of the month, and years. The exact order of these items depends on the locale setting. An example of this mode is [ November | 15 | 2007 ]. Available in iOS 2.0 and later. Declared in UIDatePicker.h. 

您可以使用UIDatePickerView,然后您可以指定从IB显示的格式。

使用UIDatePicker 苹果引用和苹果有一个很好的示例代码使用uidatepicker以及DateCell

我也build议你使用UIDatePicker。 这将是一个更好的select。

如果你不喜欢使用UIDatepicker,那么你可以试试这个只显示月份和年份的UIComponent(由我创build):

MonthSelector