2017-02-18 53 views
1

我找到了這個答案How do I setup a second component with a UIPickerView,這很有幫助,但我想做更多。我想讓第二個組件的數據依賴於第一個組件。這裏是我的代碼UIPickerView swift中的多個組件

class timerScreen: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource { 
// where all the outlets are setup 
@IBOutlet weak var pickerViewOutlet: UIPickerView! 
// where I try to set up all my variables and constants 
let cubesToWorkWith = ["3X3", "2X2", "4X4", "5X5", "6X6", "7X7", "Skewb", "Square-One"] 
let firstArray = ["cross", "OLL", "Pll", "Corners", "Edges"] 
let SecondArray = ["OLL" "Pll"] 

// where the picker view is set up. 
func numberOfComponents(in pickerView: UIPickerView) -> Int { 
    return 2 
} 

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 
    return cubesToWorkWith.count 
} 
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { 
    return cubesToWorkWith[row] 
} 
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 
    cubeSelected = Int16(row) 
} 


} 

我想如果「3X3」如果選擇了「2X2」目前在第一組分和secondArray選擇要顯示在第二部分的firstArray。你會怎麼做? 感謝您提前提供任何幫助!

回答

-1

請嘗試這樣做。我不知道你到底想要什麼。但似乎你想重新加載第一個組件的行選擇組件。如果這是要求,那麼這個代碼將起作用。

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { 
    return 2 
} 

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 
    //row = [repeatPickerView selectedRowInComponent:0]; 
    var row = pickerView.selectedRowInComponent(0) 
    println("this is the pickerView\(row)") 

    if component == 0 { 
     return cubesToWorkWith.count 
    } 
    else { 
     return firstArray.count 
    } 
} 

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! { 

    if component == 0 { 
     return cubesToWorkWith[row] 
    } else { 
     return getArrayForRow(row)[row] 
    } 
} 

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 
    if component == 0 { 
      pickerView.reloadComponent(1) 
    } 
} 

func getArrayForRow(row: Int) -> [String] { 
    switch row { // check if 2*2 0r 3*3 
    case 0: 
     return firstArray 
    case 1: 
     return secondArray 
    // and so on... 
    default: 
     return [] 
    } 
} 
+0

即時得到一個錯誤「無法將類型'[String]'的返回表達式轉換爲返回類型'String?' 「在getArrayForRow(row)嵌套在titleForRow中 –

+1

答案是錯誤的。它甚至不應該編譯。數組返回的地方是一個字符串。 ' - > String!'也沒有意義。 – shallowThought

+0

我更新了答案。實際上它的返回類型是String,並且返回一個String數組。它是一種錯字。我沒有編譯這個在Xcode或工具它只是我的估計答案給你們的方向 – iProgrammer

-1

您可以使用簡單的if/elseswitch案件挑選合適的陣列:

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {   
    if component == 0 { 
     return cubesToWorkWith[row] 
    } else { 
     if cubesToWorkWith[lastSelectedCube] == "3X3" { 
      return firstArray[row] 
     } else if cubesToWorkWith[lastSelectedCube] == "2X2" { 
      return secondArray[row] 
     } else /* You did not mention what to show for other selections, that would be handled here */ { 
      return "undefined" 
     } 
    } 
} 

您可以在同一複製到numberOfRowsInComponent並返回數組的count


完整代碼:

class timerScreen: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource { 
    // where all the outlets are setup 
    @IBOutlet weak var pickerViewOutlet: UIPickerView! 
    // where I try to set up all my variables and constants 
    let cubesToWorkWith = ["3X3", "2X2", "4X4", "5X5", "6X6", "7X7", "Skewb", "Square-One"] 
    let firstArray = ["cross", "OLL", "Pll", "Corners", "Edges"] 
    let secondArray = ["OLL", "Pll"] 
    var lastSelectedCube = 0 
    // where the picker view is set up. 
    func numberOfComponents(in pickerView: UIPickerView) -> Int { 
     return 2 
    } 

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 

     if component == 0 { 
      return cubesToWorkWith.count 
     } else { 
      if cubesToWorkWith[lastSelectedCube] == "3X3" { 
       return firstArray.count 
      } else if cubesToWorkWith[lastSelectedCube] == "2X2" { 
       return secondArray.count 
      } else /* You did not mention what to show for other selections, that would be handled here */ { 
       return 0 
      } 
     } 
    } 

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { 

     if component == 0 { 
      return cubesToWorkWith[row] 
     } else { 
      if cubesToWorkWith[lastSelectedCube] == "3X3" { 
       return firstArray[row] 
      } else if cubesToWorkWith[lastSelectedCube] == "2X2" { 
       return secondArray[row] 
      } else /* You did not mention what to show for other selections, that would be handled here */ { 
       return "undefined" 
      } 
     } 
    } 

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 
     if component == 0 { 
      lastSelectedCube = row 
      pickerView.reloadComponent(1) 
     } 
    } 
} 

由於兩個if引導的從句看起來非常相似,你現在可以重構出來的一個額外的方法:

... 
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {   
    let contentArray = content(for: component) 

    return contentArray.count 
} 

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { 
    let contentArray = content(for: component) 

    if row >= contentArray.count { 
     return nil 
    } 

    return contentArray[row] 
} 

func content(for component: Int) -> [String] { 
    if component == 0 { 
     return cubesToWorkWith 
    } else { 
     if cubesToWorkWith[lastSelectedCube] == "3X3" { 
      return firstArray 
     } else if cubesToWorkWith[lastSelectedCube] == "2X2" { 
      return secondArray 
     } else /* You did not mention what to show for other selections, that would be handled here */ { 
      return [] 
     } 
    } 
} 
...