0

我有两列pickerView。在第一列中,我有公里、英尺等单位。我想为 km 1、2、3 和英尺 100、2000、300 设置不同的值。现在我做了以下代码。我在 viewDidLoad 方法中声明了两个数组 unitArray 和 distanceArray。

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
        return 2;
}

-(NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{

if (component == UNIT)
       return [unitArray objectAtIndex:row];
else
       return [distanceArray objectAtIndex:row];
}

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

        distanceRow = [distancePicker selectedRowInComponent:DISTANCE];
        unitRow = [distancePicker selectedRowInComponent:UNIT];

        dist = [distanceArray objectAtIndex:distanceRow];
        unit = [unitArray objectAtIndex:unitRow];

        txtIBOutletDistance.text =[NSString stringWithFormat:@"%@ %@",dist,unit];
}

执行此代码后,我将显示相同的公里和英尺值,即在一列中显示公里和英尺,在第二列中显示 1,2,3。我希望在选择公里后的值应该是 1、2、3,而英尺的选择值应该是 100,200,300。如果有人知道该怎么做,请帮助我。我会感激他/她。

4

1 回答 1

0

您应该根据所选 UNIT 更改 pickerView:didSelectRow:inComponent 中 distanceArray 的值,如下所示:

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if(component == UNIT) {
        unit = [unitArray objectAtIndex:row]; //unit is a property of the interface
        if([unit IsEqualToString:@"km"] {
            distanceArray = [NSArray arrayWithObject:@"1", @"2", @"3", nil];
        } else if([unit IsEqualToString:@"feet"] {
            distanceArray = [NSArray arrayWithObject:@"100", @"200", @"300", nil];
        }
    } else {
        dist = [distanceArray objectAtIndex:row];
        txtIBOutletDistance.text =[NSString stringWithFormat:@"%@ %@",dist,unit]; 
    }
}
于 2012-05-20T12:09:20.570 回答