i tired two component in picker view i.e (currency),(id) and showed but my question is how to hide "id" component alone in picker view."currency" in responseArray and "id" in responseArray1.
picker view delegate:
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;{
return 2;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;{
return [responseArray count];
}
-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;{
//return [responseArray objectAtIndex:row];
if (component == 0) {
return [responseArray objectAtIndex:row];
} else {
return [responseArray1 objectAtIndex:row];
}
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;
{
NSLog([responseArray1 objectAtIndex:row]);
if (component == 0) {
[pickerView selectRow:row inComponent:1 animated:YES ];
} else if(component == 1)
{
if (row != [pickerView selectedRowInComponent:0])
{
[pickerView selectRow:[pickerView selectedRowInComponent:0] inComponent:1 animated:YES];
}
}
}
Answer as per comments :
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [responseArray count];
}
-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [responseArray objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSLog(#"rsponse currency : %#",[responseArray objectAtIndex:row]);
NSLog([responseArray1 objectAtIndex:row]);
// You can fetch value from both array using row parameter.
// You can display currency and id for particular row from here like above
}
You not need to display two component in pickerview. Just display one which you want to show and according to selected row you can fetch respective element from array.
Hope this will help :)
I am using a UIpicker with three components. I want if I select a row in first component on the basis of selected component it shows the value of the corresponding data in second component.
i used array for first component value like #"UK",#"US",#"EU".now i want if user selected UK in first component then i want to load UKsize1=[NSArray arrayWithObjects:#"1",#"2",#"3",#"4",#"5", nil]; in second component , if user selected US in first component then i want EUsize2=[NSArray arrayWithObjects:#"11",#"22",#"33",#"44",#"55", nil]; load in second component. same like for EU value then i want to load USsize3=[NSArray arrayWithObjects:#"111",#"222",#"333",#"444",#"555", nil];. i want second component value changed based on the first component , third component value is fixed.
this code i tried but i am not getting exact value.
- (id)init
{
if (self = [super init]) {
notesToDisplayForKey = [NSArray arrayWithObjects: #"UK", #"US", #"EU", nil];
scaleNames = [NSArray arrayWithObjects: #"11", #"22", #"33", #"555",#"666",#"777",nil];
scaleValue=[NSArray arrayWithObjects: #"1" ,#"2", #"3", #"4",#"5",#"6", #"7",#"8",#"9", #"10",#"11",nil];
UKsize1=[NSArray arrayWithObjects:#"1",#"2",#"3",#"4",#"5", nil];
EUsize2=[NSArray arrayWithObjects:#"11",#"22",#"33",#"44",#"55", nil];
USsize3=[NSArray arrayWithObjects:#"111",#"222",#"333",#"444",#"555", nil];
}
return self;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 3;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
// Returns
switch (component) {
case 0: return [notesToDisplayForKey count];
case 1:
if([selectedKey isEqualToString:#"UK" ])
{
return [UKsize1 count];
}
else if([selectedKey isEqualToString:#"US"] )
{
return [USsize3 count];
}
else
{
return [EUsize2 count];
}
//return [scaleNames count];
case 2: return [scaleValue count];
default:break;
}
return 0;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
switch (component) {
case 0: return [notesToDisplayForKey objectAtIndex:row];
case 1:if([selectedKey isEqualToString:#"UK" ])
{
selectedScale = [scaleNames objectAtIndex:row];
return selectedScale;
}
else if([selectedKey isEqualToString:#"US"] )
{
selectedScale = [USsize3 objectAtIndex:row];
return selectedScale;
}
else
{
selectedScale = [EUsize2 objectAtIndex:row];
return selectedScale;
}
case 2: return [scaleValue objectAtIndex:row];
default:break;
}
return nil;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
// NSLog(#"Row %i selected in component %i", row, component);
switch (component) {
case 0:
selectedKey = [notesToDisplayForKey objectAtIndex:row];
return;
case 1:
// selectedScale = [scaleNames objectAtIndex:row];
if([selectedKey isEqualToString:#"UK" ])
{
selectedScale = [scaleNames objectAtIndex:row];
return;
}
else if([selectedKey isEqualToString:#"US"] )
{
selectedScale = [USsize3 objectAtIndex:row];
return;
}
else
{
selectedScale = [EUsize2 objectAtIndex:row];
return;
}
return;
case 2:
selectedValue = [scaleValue objectAtIndex:row];
return;
default:break;
}
}
just check this method .it will work .
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
// NSLog(#"Row %i selected in component %i", row, component);
switch (component) {
case 0:
selectedKey = [notesToDisplayForKey objectAtIndex:row];
[pickerView reloadAllComponents];
return;
case 1:
// selectedScale = [scaleNames objectAtIndex:row];
if([selectedKey isEqualToString:#"UK" ])
{
selectedScale = [UKsize1 objectAtIndex:row];
return;
}
else if([selectedKey isEqualToString:#"US"] )
{
selectedScale = [USsize3 objectAtIndex:row];
return;
}
else
{
selectedScale = [EUsize2 objectAtIndex:row];
return;
}
return;
case 2:
selectedValue = [scaleValue objectAtIndex:row];
return;
default:break;
}
}
It seems like you are not reloading the picker....Every time you need to change the data in the picker you need to reload the picker just like UITableView.
// Reloading whole view or single component
- (void)reloadAllComponents;
- (void)reloadComponent:(NSInteger)component;
I have 3 UITextField's, date1, date2(UIDatePicker) and station name(Like combobox). First of all I must load all station name to station name UITextField.When I enter A listing starting with A station name.I select first date,second date and station name for send to web service for display fields on labels.I want to sending station ID when I select station name.Every station name have station ID.
Anybody have idea or sample ? Thanks.
if you are using picker view then try this:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return 3;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if(row == 0)
{
return #"Station 1";
}
else if(row == 1)
{
return #"Station 2";
}
else if (row==2)
{
return #"Station 3";
}
return #"";
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if(row == 0)
{
self.int_Stationid = 1;
}
else if(row == 1)
{
self.int_Stationid = 2;
}
else if (row==2)
{
self.int_Stationid = 3;
}
}
I am loading an array from a web service and displaying them in picker and all works fine. My array contains random number of questions for every category of questions. Now what i want is that i wanna add one more entry "Your own question" at the end of array so that the last row of picker displays "Your own question". How can i do this
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel *retval = (id)view;
if (!retval) {
retval= [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)];
}
// retval.text = [idAndNameArray objectAtIndex:row];
if([pickerType isEqualToString:#"picker"])
{
retval.text = [idAndNameArray objectAtIndex:row];
}
else
{
retval.text = [quesArray objectAtIndex:row];
}
retval.font = [UIFont boldSystemFontOfSize:14];
retval.numberOfLines = 3;
return retval;
}
This code only displays number of rows(questions) fetched from the web service.
EDIT 1:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component
{
if([pickerType isEqualToString:#"picker"])
{
return [idAndNameArray count];
}
else
{
return [quesArray count];
}
}
EDIT 2:
Resolved by making another NSMutableArray with my custom object "Your own question" and then appending it with the array fetched from web service
NSMutableArray *customObj = [NSMutableArray arrayWithObject: #"test"];
[quesArray addObjectsFromArray:customObj];
In UIPickerViewDataSource's method called pickerView:numberOfRowsInComponent: return your array's count plus one value(return arr.count+1;), and in the method of UIPickerVIewDelegatethat you have implemented return your label with the text when the component is greater than arr.count-1 (if(row>(arr-1)){//return the label with 'Your own question'}). Good Luck!
EDIT:
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component
{
if([pickerType isEqualToString:#"picker"])
{
return [idAndNameArray count];
}
else
{
return [quesArray count]+1;
}
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel *retval = (id)view;
if (!retval) {
retval= [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)];
}
// retval.text = [idAndNameArray objectAtIndex:row];
if([pickerType isEqualToString:#"picker"])
{
retval.text = [idAndNameArray objectAtIndex:row];
}
else
{
if(row>quesArray.count-1){
retval.text = #"You own question";
}
else{
retval.text = [quesArray objectAtIndex:row];
}
}
retval.font = [UIFont boldSystemFontOfSize:14];
retval.numberOfLines = 3;
return retval;
}
I will post my code below as a quick note I define both of the components in my .h file and I need to call an action when certain rows are picked. For example something like if row in component == 0 and other component == 1 do this?
Still new to iPhone Programing thank you in advanced!!!
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 2;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:
(NSInteger)component{
if (component==kapickerComponent) {
return [parentarray count];
}
if (component == kbpickerComponent) {
return [subarray count];
}
}
-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:
(NSInteger)rowforComponent:(NSInteger)component{
if (component == kapickerComponent) {
return [self.parentarray objectAtIndex:row];
}
if (component == kbpickerComponent) {
return [self.subarray objectAtIndex:row];
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component {
if ( row. kapickerComponent ==0, and row.kbpickerComponent==1 ??????????? ){
float number1 = ([initamount.text floatValue ]);
initamount.text = [[NSString alloc]initWithFormat:#" ", number1];
double answer = number1 *12;
result.text = [[NSString alloc]initWithFormat:#" ", answer];
}
}
Here:
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component {
if ([thePickerView selectedRowInComponent:kapickerComponent] == 0 && [thePickerView selectedRowInComponent:kbpickerComponent] == 1) {
float number1 = [initamount.text floatValue];
initamount.text = [[NSString alloc]initWithFormat:#"%f", number1];
double answer = number1 *12;
result.text = [[NSString alloc] initWithFormat:#"%f", answer];
}
}
if ( kapickerComponent.row == 0 && kbpickerComponent.row
==1){// Ur CODE HERE}