Search code examples
iosswiftuipickerview

Show Array of String in Picker View?


How to show array of strings in picker view without using didSelect delegate method ?


Solution

  • func numberOfComponents(in pickerView: UIPickerView) -> Int
    {
        return 1 // the amount of "columns" in the picker view
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
    {
        return yourArray.count // the amount of elements (row)
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
    {
        return yourArray[row] // the actual string to display for the row "row"
    }
    

    Simple as that. No didSelect needed. The didSelect delegate function is called whenever you change your selection in the picker view. You don't need that one for displaying data