UIPickerview iOS6 vs iOS7 viewForRow

时间:2014-07-19 14:43:26

标签: ios uitextfield uipickerview

我正在开发一个包含三个使用uipickerview的文本字段的视图。 pickerview是相同的,但内容是动态的,具体取决于用户正在编辑的文本字段。

如果我在iOS7的设备上运行该应用程序没有问题,但如果我在iOS6设备上运行它,当我滚动选择器视图时,我会看到旧值,并且没有显示任何行的正确值。怎么了?

这是源代码

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:   (NSInteger)component reusingView:(UIView *)view
{
UIView *pickerCustomView = (id)view;
//pickerCustomView = [[UIView alloc] init];
UILabel *pickerViewLabel;
UIImageView *pickerImageView;
NSString *testo;
UIImage *immagine;


if (mieiCalc.isEditing) {

    NSString *imgRuolo =
    [NSString stringWithFormat:@"%@%@",
     [[mieiArray objectAtIndex:row] objectForKey:@"RUOLO"],
     @".png"];
    testo = [[mieiArray objectAtIndex:row] objectForKey:@"CALCIATORE"];

    testo =
    [NSString stringWithFormat:@"%@%@%@%@",
     [[mieiArray objectAtIndex:row] objectForKey:@"CALCIATORE"],
     @" (",
     [[mieiArray objectAtIndex:row] objectForKey:@"COSTO"],
     @")"
     ];


    immagine = [variabiliGlobali imageResize:imgRuolo larghezza:30];
}
else if (squadreCalc.isEditing) {
    testo = [tutteSquadreArray  objectAtIndex:row];
    immagine = [variabiliGlobali imageResize:[variabiliGlobali getImgSquadra:testo] larghezza:30];
}
else {
    testo =
    [NSString stringWithFormat:@"%@%@%@%@",
     [[altriArray objectAtIndex:row] objectForKey:@"CALCIATORE"],
     @" (",
     [[altriArray objectAtIndex:row] objectForKey:@"COSTO"],
     @")"
     ];
    immagine = [variabiliGlobali imageResize:[variabiliGlobali getImgSquadra:[[altriArray objectAtIndex:row] objectForKey:@"SQUADRA"]] larghezza:30];

}

if (!pickerCustomView) {
    pickerCustomView= [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];

    if (!SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
        pickerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(25, 10, 30, 30)];
        pickerViewLabel= [[UILabel alloc] initWithFrame:CGRectMake(50, 10, 270, 30)];
    }
    else {
        pickerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 10, 30, 30)];
        pickerViewLabel= [[UILabel alloc] initWithFrame:CGRectMake(50, 10, 270, 30)];

    }
    pickerViewLabel.textColor = [UIColor blackColor];
    // the values for x and y are specific for my example
    [pickerCustomView addSubview:pickerImageView];
    [pickerCustomView addSubview:pickerViewLabel];
}

pickerImageView.image = immagine;
pickerViewLabel.backgroundColor = [UIColor clearColor];
pickerViewLabel.text = testo; // where therapyTypes[row] is a specific example from my code
//pickerViewLabel.font = [UIFont fontWithName:@"ChalkboardSE-Regular" size:20];

[pickerViewLabel setFont:MYFONT(20)];

return pickerCustomView;
}

1 个答案:

答案 0 :(得分:-1)

我已经用这种方式解决了。我添加了这个源代码

if (pickerImageView == nil) {
    pickerImageView = view.subviews[0];

}

if (pickerViewLabel == nil) {

    pickerViewLabel = view.subviews[1];
}

pickerview的视图被重用,因此标签和UIImageLabel没有刷新。