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:

http://imgur.com/FVvu9RA

This is using iOS6, XCode 4.6.1.

Any ideas?

6
  • How did you you know that it is second last. Jun 8, 2013 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 8, 2013 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 8, 2013 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 8, 2013 at 18:52
  • I have also tested this code and is workinf fine. Jun 9, 2013 at 11:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.