How to load data to the Picker View in Swift

October 4, 2017

In this tutorial I am going to show you how to load data to the Picker View in IOS.

First you can place the Picker View control on the ViewController

To work with Picker View you need two protocols named UIPickerViewDataSource UIPickerViewDelegate.
Now you need to conform these protocols to ViewController class

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate{

    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

You have two required methods in UIPickerViewDataSource protocol
func numberOfComponents(in: UIPickerView)
func pickerView(UIPickerView, numberOfRowsInComponent: Int)
These methods are used to specify
(a) How many components you have in your Data Scouce
(b) How many rows in your Data Source

For this example I will make a simple array to represent days of the week

var days: [String] = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]

Then I am returning the number of components in following function since I have one dimensional array I have to retun 1

  func numberOfComponents(in pickerView: UIPickerView) -> Int {
       return 1
    }

Then, returning the number of rows in array

  func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
       return days.count
    }

In addition to above methods you need one method to conform from the UIPickerViewDelegate protocol
func pickerView(UIPickerView, titleForRow: Int, forComponent: Int)
In this method I am going to return the element of the array based on the row

   func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return days[row]
    }

So now you can have a look at the completed code

import UIKit

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate{

    var days: [String] = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
       return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
       return days.count
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return days[row]
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

 

Important

You need to connect dataSource and delegate to ViewController

You can open the Connection Inspector window and add the connection as shown in the below screen

In the same manner you can connect delegate and the ViewController

Now build and run the program and you can see the following screen in your simulator