2017-03-01 83 views
0

我有一個UIPicker與兩個組件。第二個組件中的值基於第一個組件選擇進行更改。UIPicker設置默認值和更新組件

但是,如果我以編程方式設置第一個組件的值,則第二個組件不會更新。

該代碼被包含在viewDidLoad中():

UnitPicker.dataSource = self; 
UnitPicker.delegate = self; 
UnitPicker.selectRow(temporaryTask.quantityFormat.rawValue, inComponent: 0, animated: false); 
UnitPicker.reloadAllComponents(); 

if (UnitPicker.selectedRow(inComponent: 0) == 1) 
{ 
    UnitPicker.selectRow(temporaryTask.quantityUnit.rawValue, inComponent: 1, animated: false); 
    UnitPicker.reloadComponent(1); 
} 
else if (UnitPicker.selectedRow(inComponent: 0) == 2) 
{ 
    UnitPicker.selectRow(temporaryTask.quantityUnit.rawValue - 3, inComponent: 1, animated: false); 
    UnitPicker.reloadComponent(1); 
} 

下面是委託功能之一:

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int 
{ 
    if (component == 0) 
    { 
     return units.count; 
    } 
    else if (component == 1) 
    { 
     if (pickerView.selectedRow(inComponent: 0) == 1) 
     { 
      return weightTypes.count; 
     } 
     else if (pickerView.selectedRow(inComponent: 0) == 2) 
     { 
      return timeTypes.count; 
     } 
    } 

    return 1; 
} 

預期結果(當用戶選擇所述第一成分):

Expected Result

實際結果(當我設置冷杉在代碼噸分量):

Actual Result

然後,如果用戶選擇「重量」,然後「時間」,該第二部件被正確地顯示。

出於某種原因,它只顯示一行,我認爲這是因爲「返回1」;線。

預先感謝任何答覆:)

回答

1

我想你搞錯selectedRows

if (UnitPicker.selectedRow(inComponent: 0) == 1) 
else if (UnitPicker.selectedRow(inComponent: 0) == 2) 

的指數應

if (UnitPicker.selectedRow(inComponent: 0) == 0) 
else if (UnitPicker.selectedRow(inComponent: 0) == 1) 

做這個委託爲好。

+0

阿對不起,在所述第一組件和所述第二和第三的三個選項(索引1和2)那些在揭示數據第二部分。 –

0

從viewDidLoad中()來viewDidLayoutSubviews()移動代碼固定的問題:)