我有两列pickerView。在第一列我有像公里,英尺的单位。我想为km 1,2,3和foot 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];
}
执行此代码后,我显示了km和feet的相同值,即一列中的km和feet以及第二列中的1,2,3。我想在选择km值后应该是1,2,3并且对于脚的选择值应该是100,200,300。如果有人知道怎么做,请帮助我。我会很感激他/她。
答案 0 :(得分: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];
}
}