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.
Related
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.
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.
The UIPickerView class has a method selectedRowInComponent that returns the selected row for the component. That may or may not be the item that is in the center of the picker view. What I'd like is to be able to tell what index of the pickerView data is currently being displayed (not selected).
To illustrate the difference, if you tap a value in the UIPickerView dial, the row for that value seems to always be returned in selectedRowInComponent. However, if the user scrolls the picker through several values and then lifts the finger, selectedRowInComponent is not updated, or it is updated to the value that just scrolled off.
Is there anyway to determine where the picker component is actually dialed?
The value is always available in method pickerView:didSelectRow:inComponent: but I would like to query it in method pickerView:titleForRow:forComponent:
Why? Because while the UIPickerView is being scrolled selectedRowInComponent is not being called. If for some reason I get to the end of the data and need to add more, I need to do it in a method that is being called. That would seem to be pickerView:titleForRow:forComponent:, which is called for every row.
I know this question is a bit old, but by answer it I could help someone else.
I think I found a solution to this problem you had.
Solution
- (NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
// Passing the row being viewed to a function that does something with it.
[self handleRowBeingViewed:[pickerView selectedRowInComponent:component]];
}
- (void)pickerView:(UIPickerView *)pickerView
didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
// Passing the row being viewed to a function that does something with it.
[self handleRowBeingViewed:[pickerView selectedRowInComponent:component]];
}
/**
* It receives, in input 'rowBeingViewed', the row being viewed by the user in the center of the picker view, and use it to XYZW...
*
*
* #param rowBeingViewed Row currently being viewed by the user in the center of the picker view.
* #return
*/
- (void) handleRowBeingViewed:(NSInteger)rowBeingViewed
{
// Printing for debugging.
NSLog(#"String Being Viewed: %#", pickerViewArray[rowBeingViewed]);
// DO YOUR STUFF HERE
// ...
}
Explanation
The UIPickerView allows you to know which row is selected (or viewed in the center, which I think is the same) at any time by using the method selectedRowInComponent.
If you use that method selectedRowInComponent inside the delegate's method pickerView:titleForRow:forComponent, you can know which row is being viewed in the center every time a new title is being "printed" to the user in the picker view.
Sometimes, if in the Picker View you scroll only 1 or 2 rows, the delegate's method pickerView:titleForRow:forComponent does not get called and you won't know which row is being viewed in the center. To solve that, also use the same method selectedRowInComponent inside the delegate's method pickerView:didSelectRow:inComponent:.
Hope this helps someone.
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
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! :)