1

私はiOSが初めてです。PickerView年と月を表示する方法を教えてもらえますか? 各列のサイズを設定するにはどうすればよいですか?

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

}
4

2 に答える 2

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

幸運を!

于 2013-02-09T07:40:23.513 に答える
0

PickerView で年と月を表示するための優先 URL:

http://www.edumobile.org/iphone/iphone-programming-tutorials/add-datepicker-programmatically-and-display-date-in-iphone/

于 2013-02-09T11:50:25.297 に答える