UIPicker selectRow and selectedRowInComponent

Discussion in 'iOS Programming' started by LauraZ, Jul 12, 2011.

  1. LauraZ macrumors newbie

    Jul 12, 2011
    I can't for the life of me figure out why once in a while the selectedRow in my picker decrements by one for no apparent (to me) reason. I am posting two screen shots each having the same 3 parts: the relevant code, the relevant local variables, and the output screen. In one screen shot (the desired case), the behavior is what I expect. In the other screen shot, doing the same thing only in a different area of my picker, the selectedRow is decremented by one. Please reference the last two line of the output screen. I really have no clue why it would do this. I tell it what row to select and then NSLog the selectedRow and it is not what I asked for. Any insight would be greatly appreciated.

    This is my first forum post and it probably shows. I am not a real programmer and can't even claim self-taught...more like forum-taught as i would never be at it this as long as I have without all the forums help. The apple doc is good, but it seems like most of my problems are solved finding like issues in forums.


    Attached Files:

  2. jiminaus macrumors 65816


    Dec 16, 2010
    This code seems to assume that array and the items in the picker match.

    I notice that in the second screenshot that array has an extra initial element that is not present in the first screenshot.

    In the first screenshot, array has these elements.
    @"10&U", @"11-12", @"13-14", @"15-16", @"17-18"
    In the second screenshot, array has these elements.
    [COLOR=red]@"&&U"[/COLOR], @"10&U", @"11-12", @"13-14", @"15-16", @"17-18", @"senior"
    If the picker doesn't have this same extra initial element, the index to @"15-16" in the array is going to be different to the index in the picker.
  3. LauraZ thread starter macrumors newbie

    Jul 12, 2011
    still confused

    Thank you. Component1 is a dependent component, and I think I am updating its contents first before passing in the updated array each time i need to reset the picker. I am going to check that again. However, I am still confused why in any case if i select a row ("zero indexed number identifying a row in component") in the picker and then in the next line ask for the return value ("a zero-indexed number identifying the selected row") of the selectedRowInComponent from the same picker and component, then why would the component row and its index ever be different? I didn't change the picker at all between those 2 lines. Clearly, they can be different, but how?
  4. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    Not sure but the method to set the selection can be animated and you set that parameter to be YES. Maybe the current selection isn't changed until after the animation finishes.

    Where is this code being called? viewDidLoad, viewWillAppear, somewhere else? I have found that sometimes setting pickers in viewDidLoad doesn't work correctly but the same code later in viewWillAppear does work as expected.
  5. LauraZ, Jul 13, 2011
    Last edited: Jul 13, 2011

    LauraZ thread starter macrumors newbie

    Jul 12, 2011
    I call the resetPickerForArray code from the method: - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

        if (component == kStateComponent)
    		self.selectedState = [self.states objectAtIndex:row];
            self.stateDict = [self.clubDict objectForKey:self.selectedState];
            //refresh AgeGroups array for selected State
            NSArray *components1 = [stateDict allKeys];
            NSArray *sorted1 = [components1 sortedArrayUsingComparator:finderSort];
            self.ageGroups = sorted1;
            self.selectedOld = self.selectedAgeGroup;
            self.selectedAgeGroup = [self resetPickerForArray:self.ageGroups forComponent:kAgeGroupComponent];
            self.ageGroupDict = [self.stateDict objectForKey:self.selectedAgeGroup];
    The actual problem i see is in the picker method that has "reusingView" in it. Sometimes not always, the view for component 1 (middle) seems to start with the wrong selectedRow and then continues to scroll beyond the 5 rows needed for the pickerView. I can't figure out why it does this only sometimes, and I thought it might be related to my original question though i am not even sure about that.
  6. LauraZ thread starter macrumors newbie

    Jul 12, 2011

    The answer to my first question involved animated:YES. The second problem was because I should have reloaded the components first before i selected the row.

Share This Page