0

I have found similar questions here and here. I have tried the answers to these and other questions found on stackoverflow and Apple's developer forums, with no luck.

I created a fresh project to test this, and it works fine in that new project. I am iteratively removing differences between my new test project and the one exhibiting this issue. Any pointers would be most helpful.

I have a UIPickerView connected to my view controller. I want to initialize the UIPickerView to the last value selected by the user. However if this row is the last row in the picker, it shows the second-to-last row selected in the UI instead.

Meanwhile, logging shows the correct row index selected in the UIPickerView object instance.

Selecting any row besides the last row in the picker works fine.

I have tried calling the selectRow:inComponent:animated method from viewDidLoad, viewWillAppear, and viewDidAppear - none of these make any difference. I have also tried calling reloadAllComponents in various orders relative to row selection and data initialization - again, no difference.

Some code:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSParameterAssert(self.pickerView);

    // Set pickerView data
    self.pickerOptions = @[@"zero", @"one", @"two", @"three", @"four"];

    // Picker View config
    self.pickerView.dataSource = self;
    self.pickerView.delegate = self;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    NSInteger selectedIndex = ([self.pickerOptions count] -1);
    NSLog(@"About to select row: %d", selectedIndex);
    [self.pickerView selectRow:selectedIndex inComponent:0 animated:NO];
    NSLog(@"Selected value is %d", [self.pickerView selectedRowInComponent:0]);
}

#pragma mark UIPickerViewDataSource

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    NSInteger numRows = [self.pickerOptions count];
    NSLog(@"numRows: %d", numRows);
    return numRows;
}

#pragma mark UIPickerViewDelegate

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    NSString *rowTitle =  [self.pickerOptions objectAtIndex:(NSUInteger)row];
    return rowTitle;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    NSString *value = [self.pickerOptions objectAtIndex:(NSUInteger)row];
    NSLog(@"User selected value: %@", value);
    NSLog(@"index: %d", row);
}

Log output:

-[CCSelectorViewController viewDidAppear:] About to select row index: 4
-[CCSelectorViewController pickerView:numberOfRowsInComponent:] numRows: 5
-[CCSelectorViewController viewDidAppear:] Selected row index is 4

Here is the screenshot of the wrong row being selected:

https://i.stack.imgur.com/BUO1y.jpg

This is using iOS6, XCode 4.6.1.

Any ideas?

Community
  • 1
  • 1
Jen Leech
  • 136
  • 1
  • 4
  • How did you you know that it is second last. – Prateek Prem Jun 08 '13 at 13:40
  • 1
    I copied and pasted your code, and it worked fine for me. Have you done anything to modify the picker view? – rdelmar Jun 08 '13 at 15:20
  • 1
    I added a screen shot showing what the picker view UI shows in the simulator, when row with index 4 is selected. Index 4 should be the last row. As you can see, the second-to-last row is selected instead. – Jen Leech Jun 08 '13 at 18:50
  • 2
    I have not modified the UIPickerView in any way. Sounds like I should try it in a fresh project and see if it still reproduces for me. – Jen Leech Jun 08 '13 at 18:52
  • I have also tested this code and is workinf fine. – Prateek Prem Jun 09 '13 at 11:12
  • I tried this in a fresh project with just a single view, and it worked fine for me also. I guess my next step will be to try making the new project progressively more like my real application, and see if I can replicate the behavior. – Jen Leech Jun 09 '13 at 18:11

0 Answers0