iOS:如何使用PickerView

时间:2013-02-09 07:19:36

标签: ios objective-c ios6

我是iOS新手。任何人都可以帮助我如何使用PickerView来显示年月?如何设置每列的大小?

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

}

2 个答案:

答案 0 :(得分:2)

简单地覆盖委托方法,来自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中的

必须使用默认值填充选择器:

- (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];
}
祝你好运!

答案 1 :(得分:0)