This is by no means a needed thing. It would be nice if possible. I am not sure if this can be done.
I have a UIPickerView
and it will have 41-42 options. Right now I have all of my options alphabetically. I want them to be broken into groups and before each group I want it to have a title. Similar to how a TableView has sections and you can give each section a title. I want the title of each section in the picker but I don't want it to be a selectable row. For example:
Picker options:
Core (not selecteable)
Barbarian (selectable)
Bard (selectable)
several more options
APG (not Selectable)
Alchemist (selectable)
Cavalier (selectable)
several more options
continue with several more
Is this even possible?
You don't need to subclass UIPickerView to achieve this, rather implement the datasource and delegate thoughtfully.
I advise you to have a class that implements both, as a test I did this like:
As you see this class has a callback
var selected: (([String: Any]) -> Void)?
. It will only be called for electable items.The ViewController instantiate the source and set the callback:
and for completness, the views:
Instead of forcing a selection by selecting the next line, you could allow to select the group, but don't send a selection callback. But to make sure you trigger that nothing selectable is set, you should add a deselect callback.
Now you can use the callbacks
selected
anddeselected
to alter your user interface to i.e en- or disable buttons.For a complete example see this example code I just published: https://github.com/vikingosegundo/PickerWithSectionTitlesExample
Edit: As @Duncan C mentions in his answer. There isn't a built in solution for this in iOS7+. My answer provides an alternative way to accomplish your goals.
Instead you can do a little nasty hack:
This short example just shows how a solution might work. To make these not selectable picker objects you can use an attributed string as mentioned in the docs that looks different from the rest.
If a user will try to select one of these header objects you can just don't close the picker view. The user will have to choose another option, and by time, the user will learn that these objects with maybe a bold string is not selectable.
It doesn't look good, but that can you customize the way you want. You can also add images or UiView in the picker view objects.
No, I don't think a standard picker view offers that option. That said, you might be able to subclass UIPickerView and teach your custom subclass to have section dividers as you describe.
It would certainly be possible, although more work, to create your own control that acts like a picker with sections as you describe.