Customizing UIPickerView To Have A Label

Hi All,

I’m trying to create a timer and I’d like to label the selected rows with “mins” and “secs”. I’ve tried to add the label to pickerView(viewForRow) but this adds the label for all rows, not just the selected one.

Any ideas on how this apparently simply requirement can be implemented? I’m guessing one way would be to add a UILabel in the right spots on top of the picker but that seems a bit inelegant…

Thanks

Rob

PS I’m using Swift.

Hi Rob,

I agree that overlaying a label on a picker is not ideal. Here are a few things you can try:

See if the UIDatePicker can be configured in a way that meets your timer needs. I see some options for count down timer, but am not sure to what extent you timer is different.

Use 4 components instead of 2. Component 1 with have a single value of “minutes” and 3 will have “seconds”, while 0 and 2 are actually used for picking.

In UIPickerViewDelegate.didSelectRow, save which row was selected. Then in UIPickerViewDataSource.titleForRow, if row == selectedRow, you can return the value plus “Minutes”. This one still feels a bit fragile.

Good luck

That worked, thanks!

Rob