Recently I had to deal with a lot of UIPicker views in an app for a customer. In order to reduce the amount of code to write I tried to create a generic way of setting up a picker view which can represent any kind of data. There are several posts which pop up when searching for a generic UIPickerDataSource
but I wanted to mix it up with something on my own. So let's start!
The Generic Data Source
At first create a GenericRow<T>
struct which represents one row inside the UIPickerView
.
It owns type
, the data for one row of the picker and has a title
, the text to be displayed inside the picker view to represent the type
.
Let us utilize the GenericRow<T>
inside a GenericPickerDataSource<T>
class.
Declare three attributes:
originalItems
, which stores a list of the original data items this data source will representitems
, which stores a list ofGenericRow<T>
selected
, a callback which is executed when the user selects one item from the picker
Now we implement the initializer our generic data source.
It takes three arguments:
- the original data items to be selectable
- a function which generates a string based on the type of the data items
- a callback function which is executed when the user selects an item
generateRowTitle()
is used to create GenericRow<T>
instances. It uses the result of the method as it's title, which will later be displayed to the user inside the picker view.
The last step for the implementation is to make GenericPickerDataSource<T>
conform to the protocols.
UITextField Extensions
In order to further simplify the creation of a picker view, declare two extensions on UITextField
.
setupPickerField
takes a GenericPickerDataSource
and sets up the picker view for us. addDoneToolbar
creates a toolbar above the input view of the text field and adds a "done" button to it which will close the view on click.
Use it
Now everything is ready to be used. Assume you want to have a picker where users need to select a country. First create a data object which holds the data:
Create a GenericPickerDataSource<Country>
from a list of countries.
Specify how the text, which is displayed inside the picker view, is generated and the callback which is executed when the user selects a country.
When you run the code and tap on the text field you can select the country of your choice.
Conclusion
With an initial set up of a generic data source we have created an easy way of declaring picker views which can accept any kind of data throughout our application. This is especially helpful if your app is dealing with a lot of UIPickerViews
.
One thing to note here, is that the current implementation only supports one section inside the picker. We can think about improving the implementation by supporting more than one section. But this is something we could further elaborate in the future.