2

This issue is driving me crazy. I have a UIPickerView that I loaded in viewDidLoad, and I need to initialize the selected row that is to appear on the screen. My code is as follows:

self.pickerSkins.delegate = self;
[self.pickerSkins reloadAllComponents];    
NSLog(@"%d", [self.pickerSkins numberOfRowsInComponent:0]);
[self.pickerSkins selectRow:1 inComponent:0 animated:NO];

I have put this in viewDidLoad. NSLog logs a value of 2. However, the selected row that pops up is always the first row and not the second.

If I put the selectRow in viewWillAppear, it also doesn't work.

If I put the selectRow in viewDidAppear, the PickerView appears and then goes to the second row.

UPDATE: If I have more than 2 rows, the method works correctly from viewDidLoad, unless I am scrolling to the last row. The method never works in viewDidLoad or viewWillAppear if I am scrolling to the last row. Any idea why this is? Thanks.

How do I get the selected row to be the second row before the view appears?

Thanks.

Bassel
  • 452
  • 5
  • 21
  • Are you sure that self.pickerSkins is pointing to your picker? Have you logged it. When I do this in viewDidLoad, it works fine. – rdelmar Apr 20 '13 at 01:00
  • yeah it does point to it; otherwise it wouldn't log 2 rows in the picker. It is also loading the correct items in there from the data source. – Bassel Apr 20 '13 at 01:02
  • Well then, I have no idea, it works fine for me. – rdelmar Apr 20 '13 at 01:05
  • Hmm I figured out something else; don't know if you can help me now. The initialization works when I don't scroll to the bottom row. As in, if I have 2 rows, I cannot do selectRow:1. If I have 3, selectRow:1 goes to the second row correctly. Any idea what is causing this? – Bassel Apr 21 '13 at 00:56
  • No, I don't know why that still doesn't work. I tested mine with two rows, and I was able to pick row 1. But is there still a problem? Will you only have two rows? – rdelmar Apr 21 '13 at 01:28
  • well no, i might have more, but the issue is that i might want the last row selected if that's the last row the user selected before. I'm using it as a skins picker, so the user can scroll through a list of skins to change how the app looks. After searching some more, I found this post: http://stackoverflow.com/questions/13352166/uipickerview-cant-autoselect-last-row-when-compiled-under-xcode-4-5-2-ios-6 Seems like it only happens if you have autolayout on. Did you find a fix for the issue somehow? – Bassel Apr 21 '13 at 01:35
  • I didn't need a fix, so no. Mine worked fine, and I have auto layout on. I've never had to do anything special to get this to work in viewDidLoad. – rdelmar Apr 21 '13 at 01:52

0 Answers0