gpt4 book ai didi

iOS:如何使用 PickerView

转载 作者:行者123 更新时间:2023-11-29 11:03:20 29 4
gpt4 key购买 nike

我是 iOS 新手。谁能帮助我如何使用 PickerView 来显示年份和月份?如何设置每列的大小?

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 2;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{

}

最佳答案

简单地覆盖委托(delegate)方法,来自 Moonpreview 应用程序的真实示例:

#pragma mark UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 6;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
switch (component) {
case 0://cn
return 5;
case 1://yy
return 100;
case 2://mm
return 12;
case 3://dd
return 31;
case 4://hh
return 6;
case 5://tzone
return 1;
}
return 0;
}

#pragma mark UIPickerViewDelegate
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
//NSLog(@"pickerView didSelectRow:%i inComponent:%i", row, component);
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
switch (component) {
case 0://cn
{
return [NSString stringWithFormat:@"%d", kFirstCentury + row*100];
}
case 1://yy
{
return [NSString stringWithFormat:@"%02d", row];
}
case 2://mm
{
return [NSString stringWithFormat:@"%02d", row+1];
}
case 3://dd
{
return [NSString stringWithFormat:@"%02d", row+1];
}
case 4://hh
{
return [NSString stringWithFormat:@"%02d", row*4];
}
case 5://tzone
{
switch (row) {
case 1:
return @"EST";//eastern
case 2:
return @"CST";//central
case 3:
return @"MST";//mountain
case 4:
return @"PST";//pacific
default:
return @"UTC";//universal row 0
}
}
}
return @"xxx";
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
return 40.0;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
CGFloat w = 70.0;
switch (component) {
case 0://cn
{
return 80.0;
}
case 1://yy
{
return w;
}
case 2://mm
{
return w;
}
case 3://dd
{
return w;
}
case 4://hh
{
return w;
}
case 5://tzone
{
return 80.0;
}
}
return w;
}

创建选择器:

picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 160.0, 480.0, 160.0)];
picker.delegate = self;
picker.dataSource = self;
picker.showsSelectionIndicator = YES;
[self.view addSubview:picker];

在 Moonpreview ii 中必须使用默认值填充选择器:

- (void)setPickerDefaults {
//defaults
//calculate current values
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit) fromDate:today];
NSInteger year = [components year];
NSInteger month = [components month];
NSInteger day = [components day];
NSInteger hour = [components hour];
NSLog(@"setPickerDefaults year: %i month: %i day: %i hour: %i", year-2000, month, day, hour);
cn = [@"2000" retain];
yy = [[NSString stringWithFormat:@"%02d", year - 2000] retain];
mm = [[NSString stringWithFormat:@"%02d", month] retain];
dd = [[NSString stringWithFormat:@"%02d", day] retain];
hh = [[NSString stringWithFormat:@"%02d", ceil((hour / 4)) * 4] retain];
tzone = [@"0" retain];
[picker selectRow:3 inComponent:0 animated:YES];
[picker selectRow:year-2000 inComponent:1 animated:YES];
[picker selectRow:month-1 inComponent:2 animated:YES];
[picker selectRow:day-1 inComponent:3 animated:YES];
[picker selectRow:ceil((hour / 4)) inComponent:4 animated:YES];
[picker selectRow:0 inComponent:5 animated:YES];
[gregorian release];
}

祝你好运!

关于iOS:如何使用 PickerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14785895/

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