【问题标题】:How to prepare UIPickerView with months and years in ios?如何在 ios 中准备几个月和几年的 UIPickerView?
【发布时间】:2013-11-20 07:11:32
【问题描述】:

我正在为 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];

选取器视图委托方法:

// 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;
}

我得到以下输出:

但是在今年,从一月到十月的月份已经到期。如何在我的选择器中动态禁用当前年份的那些年份。这些月份应该可以用于剩余的年份。

其实真正的输出是,

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

我们将不胜感激。

提前致谢。

【问题讨论】:

    标签: ios objective-c uipickerview uidatepicker


    【解决方案1】:

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

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

    下面是我的代码。

    在 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];
    

    选取器视图委托方法:

    // 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 年:

    其他年份:

    .

    .

    感谢您的贡献。

    【讨论】:

      【解决方案2】:

      如果您的问题提到了date and year,那么看起来UIDatePickerModeDate 就足够了。

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

      原始答案

      您可以通过在右侧栏中的属性检查器中将Date Picker 下的Mode property 更改为日期(Cmd + Option + 4)。您也可以以编程方式执行此操作,

        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.
      

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          【解决方案4】:

          使用 UIDatePicker apple reference 并且苹果有一个非常好的示例代码来使用 uidatepicker 以及 DateCell

          【讨论】:

            【解决方案5】:

            我还建议您改用 UIDatePicker。这将是一个更好的选择。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2021-06-28
              • 2011-09-06
              • 1970-01-01
              • 2012-03-12
              • 1970-01-01
              • 1970-01-01
              • 2016-11-04
              • 2016-11-17
              相关资源
              最近更新 更多