3

enter image description here

How can we achieve such type of selected values to be changed with mins and secs in UIPickerView???

DShah
  • 9,768
  • 11
  • 71
  • 127

2 Answers2

4

First, you use the pickerView:viewForRow:inComponent:reusingView: data source method to return the "00" label. This is actually a bit tricky, since pickerview will alter the frame of this view to fill the row. That means you have to embed your properly positioned view inside another view, and then return that container view instead.

As for the labels, those are simply UILabel objects that have been positioned correctly and added as subviews of the pickerview itself.

And to get them to update correctly, just change their titles in the pickerView:didSelectRow:inComponent: method.

This is exactly what UIDatePicker is doing.

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
  • can you please give steps to be followed with this two methods.. actually i tried to change name at didSelectRow.. but not succeeded.. – DShah Sep 02 '11 at 17:32
  • Adding a UILabel in `pickerView:viewForRow:inComponent:reusingView:` method also creates the same problem i.e. it creates label on all the values instead of selected row. – DShah Sep 03 '11 at 04:32
0

You should implement UIPickerViewDelegate Protocol and UIPickerViewDataSource Protocol.

In method - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component just return needful title.

You can also add delegator on valueChanged event of UIPickerView. And each time your value changed you can reload all visible rows and set labels mins and secs at right positions.

Oooor you can just add subview with 2 UILabels over the black line.

Oooor you can add subview with black gradient line with labels mins and secs.

Then using method : - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component you can set appropriate width of your columns.

Nekto
  • 17,837
  • 1
  • 55
  • 65
  • I have thought of adding UILabels as we are doing in UITableView in creating cell... but in this case how to achieve that i am not getting.. – DShah Sep 02 '11 at 13:18
  • Why you cannot just add to your `UIPickerView` subview? `UIPickerView *picker; [picker addSubview:labelWithMinAnsSec];` – Nekto Sep 02 '11 at 13:23
  • Adding a UILabel in `pickerView:viewForRow:inComponent:reusingView:` method also creates the same problem i.e. it creates label on all the values instead of selected row. – DShah Sep 03 '11 at 04:31
  • No, you should add label when you are creating UIPickerView, not in its methods. – Nekto Sep 03 '11 at 07:35
  • Adding label?? how to do that?? I have tried adding label using addSubview method... still not successful..... – DShah Sep 03 '11 at 15:22
  • Please, show me your code that adds `UILabel` to `UIPickerView` – Nekto Sep 03 '11 at 15:51