I use UIPickerView
and its method pickerView(_:titleForRow:forComponent:)
to fill the rows. Everything works fine and I can see the rows via UI. I'm wondering how many times method is called - for example I have one component and 3 rows and if I trace method calls via print command I find 8 calls (runs).
I wanna do specific action for each row but just one time. Any hint what I did wrongly and how I should do it better? Thanks.
pickerView(_:titleForRow:forComponent:)
is called by the picker view when it needs the title to use for a given row in a given component. So whenever the title is needed that delegate function is called. And as you may have noticed these methods can be called multiple times, which is normal.
In your:
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
You have the row
, you could create a dictionary to check if you have set a value before. Something like this:
var dictionary = [Int: Bool]()
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if let value = dictionary[row] {
if !value {
// set value, the value of row has not been set before
...
// set that rows value to true so that you know it has been set
dictionary[row] = true
}
}
return "some string"
}