I'm reading a Swift OOP book and I understand the idea of instance methods having arguments which will be used within the function. What is unclear is while following online tutorials for UIPickerViews and UITableViews, there are methods that have UIPickerView or UITableView objects as parameters but aren't used in the function.
For example:
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
// Return the number of rows of data
return gamesList.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return gamesList[row].name
}
The first parameter pickerView which takes a UIPickerView object isn't used within these functions. I'm wondering why have them as parameters in the method's signature but these objects are rarely used in tutorials? Or am I thinking about this incorrectly?
Thank you in advance for any help to get a better understanding.
The first object from all the Delegate
and Datasource
methods that you are talking about is not unused object, it will hold the reference of the current UIPickerView
, So it will used if you have multiple UIPickerView
in the same ViewController
. Same thing goes for all the others control like UITableView
, UICollectionView
etc.
For eg if you have 2 UIPickerView
in the same Controller
then you can fill the UIPickerView
by comparing it in the UIPickerView
methods.
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView == firstPickerView {
return gamesList1.count
}
//else return for second pickerView
return gamesList2.count
}
So you need to compare the pickerView
reference in all the methods of UIPickerView
and fill or access the data according to it.