0

I have a ViewController with the fallowing viewDidLoad:

- (void)viewDidLoad
{
    [super viewDidLoad];

    model = [GlobalDataModel sharedDataModel];

    programTypes = @[@"Gewichtsreduktion", @"Verdauungs-/Stoffwechselprobleme", @"Energielosigkeit, Müdigkeit",
                     @"Stress, Burn-out", @"Unruhe, Schlaflosigkeit", @"Immunsystem, Hautprobleme", @"Sport - Muskelaufbau", @"Sport - Ausdauersport", @"Sport - Leistungssport"];

    int row = 8; //[model.infoDictionary[@"programmtyp"] intValue];
    [programTypePicker selectRow:row inComponent:0 animated:NO];
}

and this delegate-methods for the UIPickerView:

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {       
    return programTypes.count;
}

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

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row
            forComponent:(NSInteger)component {
    return programTypes[row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    model.infoDictionary[@"programmtyp"] = [NSString stringWithFormat:@"%ld",(long)row];
}

The problem is, that in the viewDidLoad, when I set row = 8 it will always select not the last row - instead the second-last row (instead of "Sport - Leistungssport" it select "Sport - Ausdauersport"). When I use a row smaller then 8 it will work correctly.

Does somone can help me, what I am doing wrong? - Thank you.

BennoDual
  • 5,865
  • 15
  • 67
  • 153

3 Answers3

3

This is a bug in iOS 6 with autolayout. The only workarounds I could figure out were to disable autolayout or set the picker selection in viewDidAppear: instead of viewDidLoad or viewWillAppear:.

See the following for more info: UIPickerView can't autoselect last row when compiled under Xcode 4.5.2 & iOS 6

Community
  • 1
  • 1
user1819329
  • 411
  • 3
  • 9
0

From what I could figure,

you have 9 objects in your array.

Arrays are indexed 0 to n-1 (in your case 0 to 8, not 1 to 9)

its selecting the correct row, you are not setting the right index.

If you want to make it select the second last even if the number of strings/objects in the array vary, just set it to

int row = [programTypes count]-2; //[programTypes count]-1 is the last row.
Nitin Alabur
  • 5,812
  • 1
  • 34
  • 52
  • but I want to select the last row. So, when I have 9 elements in the array, with row=8 it should select the last row, right? - But it does always select the second last row with row = 8 :-( – BennoDual Jun 23 '13 at 18:17
  • are you sure your numberOfRowsInComponent is returning the correct number and titleForRow is returning the correct title. cant figure why it'd go wrong. – Nitin Alabur Jun 23 '13 at 18:46
0

In your code snippet the variable programTypes.count returns count as 9 and you set the selectedRow as 8 so the picker selects the last before row.

Lalith B
  • 11,843
  • 6
  • 29
  • 47
  • like the answer from calvinBhai, I know, that arrays are indixed from 0. So, when I have 9 elements, it should select the last row when I use index 8 for row. But it selects the second last row :-( – BennoDual Jun 23 '13 at 18:18