How can I get multiple arrays into UIPickerview - iphone

I have an problem in UIPickerview, it is displayed in this screenshot.
Screenshot of my UIPickerView
I have three arrays. Each array is passed into UIPickerview. I can display the three columns in UIPickerview.
My problem is that the arrays are not passed into each column and are not passed into the sub-columns.
How can I solve the problem? Please help me.

In UIPickerView delegate:
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent (NSInteger)component
check for
if(component == 0)
{
//use Array1
}
else if(component == 1)
{
//use Array2
}
else if(component == 2)
{
//use Array3
}

Related

UIpickerview containing 2 component, second should cotain 1component minus selected row ios

pickerview containing 2 component both having same data when I select first component then the selected component should not appear in second component
After you make a selection in the first component, remove the item from the datasource for the second component & reload the picker components. HTH.
Your question is not clear but on the basic of what i understood:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
string = [fistPickerArray objectAtIndex:row];
//do what ever logic you want
for(NSString str in secondArray)
{
if([string isEqualToString:str])
{
[secondPickerArray removeObject:str],
}
}
[picker reloadAllComponent];
}
write below code in didSelectRow method
if(component == 0) {
string = [yourarray objectAtIndex:row];
}
then in titleForRow method,
if(component == 1) { if([string isEqualToString:[yourarray objectAtIndex:row]]) { break; return nil; else{ return string; }
}
}

How to create multiple uipickerview in tableview?

PickerView delegate method...
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (didSelectedRow ==1)
{
strChkDistanceValue = [arrDistance objectAtIndex:row];
}
else
{
strChkDateRangeValue = [arrDateRange objectAtIndex:row];
}
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
if(didSelectedRow ==1)
{
return [arrDistance count];
}
else
{
return [arrDateRange count];
}
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
if(didSelectedRow ==1)
{
strChkarray =#"arrDistance";
return [arrDistance objectAtIndex:row];
}
else
{
strChkarray =#"arrDateRange";
return [arrDateRange objectAtIndex:row];
}
}
above code I am use...
I am using the pickerview in tableview... there are two picker views...
when i am use the first picker view the first picker view array shows in picker when i am select 3rd row in picker view after i select the 2nd picker view then show the second array list but second array list show start with 3rd row...same to 1st picker...
but i want to show at first row of array in pickerviw..
please help me...
Thanks & regards
Rahul Virja
You should use below method of UIPickerView to select a particular row.
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated;

how to create two multiple UIPickerviews in single view controller

I need to create two UIPickers Dynamically with different input values.
I did n't find correct solution in google.
Can any pls post some code.
Thank u in advance.
There are two approaches to this.
a) First, make the view controller the delegate and data source of both the pickers. Then in the view controller's implementation do something like this:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return ( pickerView == picker1 ? 2 : 3 );
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
NSArray *values = ( pickerView == picker1 ? values1 : values2 );
return [values count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSArray *values = ( pickerView == picker1 ? values1 : values2 );
return [values objectAtIndex: row];
}
Note that you compare the pickerView parameter to an instance variable pointing to one of your picker views and decide "on the fly" which values to return for each of the picker views.
b) Assign different data sources and delegates to each of the picker views (they may be any objects implementing UIPickerViewDelegate and UIPickerViewDataSource protocols, not necessarily the view controller).

How do I add multiple components to a PickerView?

It may be a simple question but how do I add multiple components to a UIPickerView? I use NSMutableArray to populate one component but I dont know how to populate the others. I also need to change the value of a label when a row is selected.
Thanks in advance
Kieran
I take it that you are a beginner. Here's how to implement the methods nacho has rightly pointed out:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
// return the number of components required
return 2;
}
You can use other NSMutableArray's to populate the components. Assuming that you have 2 components, each using different NSMutableArray i.e. array1 and array2:
// Return row count for each of the components
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (component == 0) {
return [array1 count];
}
else {
return [array2 count];
}
}
// Populate the rows of the Picker
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
// Component 0 should load the array1 values, Component 1 will have the array2 values
if (component == 0) {
return [array1 objectAtIndex:row];
}
else if (component == 1) {
return [array2 objectAtIndex:row];
}
return nil;
}
Use this code to change the text of a label, say selectedValue when a value is selected in the PickerView:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
selectedValue.text = [NSString stringWithFormat: #"%#, %#", [array1 objectAtIndex:[myPicker selectedRowInComponent:0]],[array2 objectAtIndex:[myPicker selectedRowInComponent:1]]];
}
... and you are good to go :)
You might also want to see http://joshhighland.com/blog/2009/09/17/uipickerview-spinning-multiple-components/ for valuable tips on pickerView programming.
You need to set the datasource and use
– numberOfComponentsInPickerView:
– pickerView:numberOfRowsInComponent:
set its delegate and use:
– pickerView:didSelectRow:inComponent:
to change your label when a certain row in a certain component is selected. Very likely to UITableViewDelegate and Datasource
and also taking a glance to documentation helps :)
Instead of component I found using tag more proper.
Then in the code:
// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
//NSLog(#"Component_1: %d",pickerView.tag);
if (pickerView.tag == 1)
return [self.arrOpponentTeams count];
else if (pickerView.tag == 2)
return [self.arrMyTeams count];
else
return [self.arrPlayers count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
//NSLog(#"Component_2: %d",pickerView.tag);
if( pickerView.tag == 1 )
return [self.arrOpponentTeams objectAtIndex:row];
else if (pickerView.tag == 2)
return [self.arrMyTeams objectAtIndex:row];
else
return [self.arrPlayers objectAtIndex:row];
}
I do not know if it's a little late But I solved this another way Array are a bit rudimentary But it works wonders You can even put different color to each array
- (void) viewDidLoad {
horPickerArray = #[#"00",#"01",#"02",#"03",#"04",#"05".to..24];
minPickerArray = #[#"00",#"01",#"02",#"03",#"04",#"05".to..59];
secPickerArray = #[#"00",#"01",#"02",#"03",#"04",#"05".to..59];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 3;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component == 0) {
return _HorPickerArray.count ;
}
else if (component == 1) {
return _MinPickerArray.count ;
}
else if (component == 2) {
return _SecPickerArray.count ;
}
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component == 0) {
return [_HorPickerArray objectAtIndex:row];
}
else if (component == 1) {
return [_MinPickerArray objectAtIndex:row];
}
else if (component == 2) {
return [_SecPickerArray objectAtIndex:row];
}
return nil;
}
And Print in TextField
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
_OficialTime.text =[NSString stringWithFormat: #"%#:%#:%#", [_HorPickerArray objectAtIndex:[_TimerPiker selectedRowInComponent:0]],[_MinPickerArray objectAtIndex:[_TimerPiker selectedRowInComponent:1]], [_SecPickerArray objectAtIndex:[_TimerPiker selectedRowInComponent:2]]];
}
Here is the swift3 code.Here I am assigning values of multiple picker view to a UITextField.
var pickOption = [["1", "2", "3","4","5","6","7","8","9"], ["0","1","2", "3","4","5","6"],["0","1","2", "3","4","5","6"]]
#IBOutlet weak var travellersTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
let pickerView = UIPickerView()
pickerView.delegate = self
travellersTextField.inputView = pickerView
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return pickOption.count
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickOption[component].count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickOption[component][row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let adult = pickOption[0][pickerView.selectedRow(inComponent: 0)]
let child = pickOption[1][pickerView.selectedRow(inComponent: 1)]
let infant = pickOption[2][pickerView.selectedRow(inComponent: 2)]
travellersTextField.text = adult + " adults," + child + " children," + infant + " infants"
travellersTextField.becomeFirstResponder()
}

Using a UISegmentedControl to switch between multiple arrays using a uipickerview

I am working on a core data using app and one of the views is EditingViewController, which acts as the controller for a number of ui elements which describe the attributes of objects. Inside the EditingViewController, all of my ui elements are being called and hidden with the .hidden = YES/NO; operation. One of my ui elements is a uipickerview. Currently, there is one array for each of two different views. Meaning that if they clicked on the first name field, array1 would load in picker1, and they would pick from it, and then if they clicked on last name, array2 would load in picker2, and life is good. Here is my code I am using to make this work up until now:
- (NSString *)pickerView:(UIPickerView *)picker titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
if (picker1 == self.picker) {
return [array1 objectAtIndex:row];
} else {
return [array2 objectAtIndex:row];
}
}
But! array1 is so big that I would like to add a UISegmentedControl to picker1 so that it can sort array1's info a little better. I know that UISegmentedControl is just a pretty set of buttons, requiring IBActions to be linked and stuff, but how could I implement it so that in one of my views, the first one, just picker1 and array1 are managed by the UISegmentedControl? Is it possible to make it so that it is just a smooth sorting process? I figure I will break down array1 into other arrays based on the conditions I set in my UISegmentedControl, how do I link those so they look and work good? Any help on this topic as always is greatly appreciated! Thanks
Use the selectedSegmentIndex property.
For example:
enum {
SEGMENT1,
SEGMENT2,
SEGMENT3
};
- (NSString *)pickerView:(UIPickerView *)picker titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
if (picker1 == self.picker) {
if (segmentedControl.selectedSegmentIndex == SEGMENT1) {
return [array1sub1 objectAtIndex:row];
} else if (segmentedControl.selectedSegmentIndex == SEGMENT2) {
return [array1sub2 objectAtIndex:row];
} else if (segmentedControl.selectedSegmentIndex == SEGMENT3) {
return [array1sub3 objectAtIndex:row];
}
} else {
return [array2 objectAtIndex:row];
}
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (picker1 == self.picker) {
if (segmentedControl.selectedSegmentIndex == SEGMENT1) {
return [array1sub1 count];
} else if (segmentedControl.selectedSegmentIndex == SEGMENT2) {
return [array1sub2 count];
} else if (segmentedControl.selectedSegmentIndex == SEGMENT3) {
return [array1sub3 count];
}
} else {
return [array2 count];
}
}
Also, you will want to reload the picker when the segmented control changes, so you should link the segmented control's value-changed event with a method like this:
- (IBAction)handleValueChanged
{
[self.picker reloadAllComponents];
}