gpt4 book ai didi

ios - 如何在 ios 中准备带有月份和年份的 UIPickerView?

转载 作者:可可西里 更新时间:2023-11-01 05:03:18 25 4
gpt4 key购买 nike

我正致力于在 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];

选择器 View 委托(delegate)方法:

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

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

实际上真正的输出是,

enter image description here

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

如有任何意见或建议,我们将不胜感激。

预先感谢您。

最佳答案

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

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

下面是我的代码。

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

选择器 View 委托(delegate)方法:

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

enter image description here

其他年份:

enter image description here

.

.

.感谢您的贡献。

关于ios - 如何在 ios 中准备带有月份和年份的 UIPickerView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20089618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com