Search code examples
swiftuipickerview

UIPickerView custom title


I have an array that I use to populate my pickerView, a simple string array. The issue is that I need the first item to be something like "Choose team".

The only way I have achieved that is the add the first item in the array to be "Choose team", but this messes up the array structure for me and I wonder if there is another way of doing this.

So, can I add a default value to a UIPickerValue, if no: how would you have solved this issue?


Solution

  • There is no easy way to add a default value. You can duplicate the array and add the string to the duplicated version for display but then you have to be careful about indexing issues. If the array's value changes programmatically, it can be a disaster. This duplication thing is not a good design anyway. Alternatively, you can add a fixed label on right side of each picker values. Here is the example Fixed labels in the selection bar of a UIPickerView