Can I programmatically scroll to a desired row in UIPickerView? - ios

By default the first row is highlighted after initializing the UIPickerView. How do i highlight a particular row or scroll to particular row programmatically?

As always, this is thoroughly documented. The Apple documentation for UIPickerView should tell you that the method you probably want is – selectRow:inComponent:animated:.

If you want to trigger delegate's method pickerView:didSelectRow:inComponent, you should call it manually:
Obj-C
[self.myPickerView selectRow:0 inComponent:0 animated:NO];
[self pickerView:self.myPickerView didSelectRow:4 inComponent:0];
Swift
self.myPickerView.selectRow(0, inComponent: 0, animated: false)
self.pickerView(self.myPickerView, didSelectRow: 0, inComponent: 0)

Yes, it's very easy [picker selectRow:row inComponent:component animated:NO];

Working in iOS 9 with XCode 7.3, to make runtime changes to data in a picker view called fieldPicker:
// first load your new data to your picker view's data source (not shown here...) then load the data into the picker view using it's delegate
[self.fieldPicker reloadAllComponents];
// now that the data is loaded, select a row in the picker view through it's delegate
[self.fieldPicker selectRow:([self.theFields count] - 1) inComponent:0 animated:NO];
// retrieve the row selected in the picker view to use in setting data from your data source in textboxes etc.
long row = (long)[self.fieldPicker selectedRowInComponent: 0];

Check if you call the method self.myPickerView.selectRow(0, inComponent: 0, animated: false) after you added the pickerView as a subview.
First, I called the method before I added the pickerView as a subview and it did not work. Therefore, I called it afterwards and then it worked!
Probably pretty obvious and not my smartest move but still maybe someone has the same problem some day and I hope I could help! :)

Related

Different picker data for every cell

I have a table view with custom cells, each cell contains a textField. When the user tap on the textfield a UIPickerView will be displayed. I have this approach already done and work good. The thing is, the picker should display different data depending on which textField has been tapped, how can I detect this?
What I've done:
In viewDidLoad I create a view property which contains a UIPickerView.
In cellForRowAtIndexPath: I assign that view with the picker as an inputView for the textField (I did it this way to avoid creating a picker everytime a cell is rendered)
Ok, I found a solution that works really smooth:
I disabled the textField on every cell
I stop adding the picker as an inputView in cellForRowAtIndexPath
Once the user tap on didSelectRowAtIndexPath (which is called when they tap on the textField) I do the following:
if ([dataInfo[indexPath.row] isEqualToString:#"Accessibility"]) {
pickerItems = accessibilityItems;
}
else if ([dataInfo[indexPath.row] isEqualToString:#"Floor"]){
pickerItems = floorItems;
}
[picker reloadAllComponents];
SubCellTableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
[cell.subcellSubtitleTextField setEnabled:YES];
cell.subcellSubtitleTextField.inputView = viewContainingPicker;
[cell.subcellSubtitleTextField becomeFirstResponder];
I hope this can help someone else!

UIPickerView: select one element after the other finishes animation

I have a UIPickerView with two elements: months and days. I am doing this
[self.picker selectRow:3 inComponent:0 animated:YES];
[self.picker selectRow:10 inComponent:1 animated:YES];
This makes both elements to roll to the selected items.
But In fact what I want to do is to roll the first element and then, when the first element finishes rolling, roll the second element.
I can do a dispatch after delay but this is a hack.
Is there any way to know when the first animation ends and then trigger the second?
Implement the delegate method pickerView:didSelectRow:inComponent:.
Call selectRow on the fist picker, then when didSelectRow is called with row 3 for the first picker, call selectRow on the second picker.

how to create tabs that expands when selected in iOS

I want to create a tableView or tabs that expands when user selects them. This is very commonly used in webpages using jquery.
you can check http://jqueryui.com/accordion/ It does exactly what i want to do in my ipad app. with horizontal tabs too http://jqueryui.com/tabs/
Can anyone tell me how to achieve this in my iPad app ? Or any pointers would be appreciated.
TIA
Sam
1.Use UITableView for first type of tab.
a) Use Header and Cell View in your desired format.
b) Hide and unhide cell view with some animation.
2.Use UISegementedControl for second type of tab.
a) Use function addTarget :
[self.mySegmentedControl addTarget:self action:#selector(segmentChanged) forControlEvents:UIControlEventValueChanged];
b) Implement segmentChanged :
- (void) segmentChanged:(UISegmentedControl *)paramSender{
//check if its the same control that triggered the change event
if ([paramSender isEqual:self.mySegmentedControl]){
//get index position for the selected control
NSInteger selectedIndex = [paramSender selectedSegmentIndex];
if (selectedIndex == 1 ) { // do required } and so on....
}
}
The best possible way is using the headerview to show the Master row and the tableview cells for showing the Details row.Write a touch event for the headerview and set it with the showing and hiding the cells ,with neat row animation.You need to keep the status of the opened and closed state of cell with an array of bools repesenting the no of masterview.

UIPicker interaction between them

I have 2 pickerviews on screen. Both populate rotate and taps return
correct and expected results. They are distinguished by the TAG
property using the "if/switch" statement in the necessary delegate
methods.
What I'd like is forpicker1 to change the selected row in picker2
dynamically during runtime as the user interacts.
E.g.
picker1 has values 1 to 10
Picker2 has values red, blue, green, purple etc
User taps row with value 2 in picker1 and automatically picker2
rotates/animates to predetermined row say purple in this case, user then taps picker1 again row 5 and again picker2 is animated/rotated to another predetermined row, and so on
What works:
The two pickers are created in viewDidLoad and
[picker2 selectRow:row inComponent:component animated:NO];
works with no issues, but only first time round at initial view load.
Its does not change the selected row if called from
What does NOT work is calls to:
[picker2 selectRow:row inComponent:component animated:NO]; from other (delegate) methods, even if I call:
[self.thePicker reloadAllComponents];
Does this work at all, or is it not supposed to do this? I'm new to this forum, so apologies in advance if I'm being thick! I have trolled look for the answer though
Cheers

UIPickerView selectRow not working

I thought this would be simple, however....
All I want to do is select a row in a picker view on a button press. But this doesn't work.
int row;
row = [self.teamList indexOfObject:lastEvent.TeamWon];
[pickVRPossession selectRow:row inComponent:0 animated:NO];
[pickVRPossession reloadAllComponents]; //I tired this before and after selectRow but makes no difference
I know it's returning the correct index but it always remains on the first (or previously selected item) in the list. Any and all help appreciated.
The [pickVRPossession reloadAllComponents] would cause the UIPicker to reload data from the Delegate and lose your selection from the line of code before.
Lose the last line of your code snippet and you should be good to go.

Resources