iOS UIPickerView calls numberOfRowsInComponent even UIPickerViewDataSource is not bound

945 views Asked by At

Hi, this question is only about for curiosity.

I have UIPickerView in my controller, delegate is bound from IB and implemented in controller, and also one of UIPickerViewDataSource method is implemented in controller numberOfRowsInComponent. This method get called even dataSource is not bound and implemented completely. I wonder how it's possible? One of my thought is, this method called from background implicitly... My code;

class ViewController: UIViewController, UIPickerViewDelegate {

let titles = ["item 1", "item 2", "item 3", "item 4", "item 5"]

override func viewDidLoad() {
    super.viewDidLoad()
}

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

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

There are 1 answers

3
Vishal Sonawane On

As you have mentioned in your comment that you have set delegate/ datasource from storyboard, then its equivalent to pickerView.delegate = self / pickerView.datasource = self.

You can assign delegate and datasource either by coding or from storyboard. So in your case you have done it from storyboard, so its actually bound with your pickerView.