Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Guys i am facing an issue while implementing delegate method for UIPickerview. Description of the problem is, i want to have 3 components in pickerview and each component having different number of rows. The code for this is as follows

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;

}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

{   
    NSInteger rows;
    if (0 == component)
        rows = 5;
    else if (1 == component)
        rows = 10;
    else
        rows = 15;

    NSLog(@"Number of rows returned for component %d are rows %d",component, rows);
    return rows;
}

Following this i am implementing "titleForRow" delegate method code for which is as follows:

#pragma -mark UIPIckerView Delegate Methods
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
        //NSString *temp;


        //temp = [[NSString alloc] initWithString:[self.pocModelData.arrayOfDistricts objectAtIndex:row]];
    if (0 == component)
    {
    NSLog(@"String returned is %@ for row %d for component %d", [self.pocModelData.arrayOfDistricts objectAtIndex:row], row, component);
    return [self.pocModelData.arrayOfDistricts objectAtIndex:row];
    }
    else if
        (1 == component){
        NSLog(@"String returned is %@ for row %d for component %d", [self.pocModelData.arrayOfDistricts objectAtIndex:row], row, component);
    return [self.pocModelData.arrayOfDistricts objectAtIndex:row];
    }else{
        NSLog(@"String returned is %@ for row %d for component %d", [self.pocModelData.arrayOfDistricts objectAtIndex:row], row, component);
    return [self.pocModelData.arrayOfDistricts objectAtIndex:row];
    }
        //return temp;
        //  return [self.pocModelData.arrayOfDistricts objectAtIndex:row];
}

This method on execution is returning only 3 rows for each component, as shown by following statements

2013-05-18 02:00:40.743 PopOverControllerExample[8228:c07] Number of rows returned for component 0 are rows 5
2013-05-18 02:00:41.221 PopOverControllerExample[8228:c07] Number of rows returned for component 1 are rows 10
2013-05-18 02:00:41.696 PopOverControllerExample[8228:c07] Number of rows returned for component 2 are rows 15
2013-05-18 02:00:42.035 PopOverControllerExample[8228:c07] Number of rows returned for component 0 are rows 5
2013-05-18 02:00:43.761 PopOverControllerExample[8228:c07] String returned is 1 for row 0 for component 0
2013-05-18 02:00:44.687 PopOverControllerExample[8228:c07] String returned is 2 for row 1 for component 0
2013-05-18 02:00:45.048 PopOverControllerExample[8228:c07] String returned is 3 for row 2 for component 0
2013-05-18 02:00:45.544 PopOverControllerExample[8228:c07] Number of rows returned for component 1 are rows 10
2013-05-18 02:00:46.131 PopOverControllerExample[8228:c07] String returned is 1 for row 0 for component 1
2013-05-18 02:00:46.620 PopOverControllerExample[8228:c07] String returned is 2 for row 1 for component 1
2013-05-18 02:00:47.380 PopOverControllerExample[8228:c07] String returned is 3 for row 2 for component 1
2013-05-18 02:00:47.772 PopOverControllerExample[8228:c07] Number of rows returned for component 2 are rows 15
2013-05-18 02:00:48.543 PopOverControllerExample[8228:c07] String returned is 1 for row 0 for component 2
2013-05-18 02:00:48.915 PopOverControllerExample[8228:c07] String returned is 2 for row 1 for component 2
2013-05-18 02:00:49.461 PopOverControllerExample[8228:c07] String returned is 3 for row 2 for component 2

I am not able to comprehend as to why its executing only 3 times for each component? I am populating the rows with set of data, so data source is common for all of them. What is the issue here, i am not able to grasp and m stuck for some time now? Any guidance is highly appreciated and thanks in advance.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

It only queries the values that are visible. With row 0 at the center of the spinner, you have 3 rows visible. Once you spin it will query the other values.

share|improve this answer
    
thnks @mvds, but can you elaborate a bit more? Correct me if i am wrong, but do u mean, that its returning 3 values only cause, there are only 3 rows visible? –  experimentalist May 17 '13 at 20:51
1  
@experimentalist yes, as more values need to be displayed, they are requested. Just spin the picker and you will see that other values are requested. –  mvds May 17 '13 at 20:53
    
thanks a lot for the explanation..it helped me in resolving my issue!!:) –  experimentalist May 18 '13 at 21:01
    
sorry took some time to update and acknowledge that ur explanation solved my query. kinda of new to posting questions, so still a noob –  experimentalist May 20 '13 at 16:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.