Search code examples
iphoneiosscrolluipickerview

How to scroll multiple component together in UIPickerView?


I have a problem related to scrolling multiple components of UIPickerView. I created uipickerview with two components but when i scroll one of them, other also scroll but after part of second.

Code :

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

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
       return [finalArray count];
}


-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
if (component == 0) {
    return [[finalArray objectAtIndex:row] objectForKey:@"value1"];
}else if (component == 1){
    return [[finalArray objectAtIndex:row] objectForKey:@"value2"];
}else{
    return nil;
}

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
        [thePickerView selectRow:row inComponent:0 animated:NO];
        [thePickerView selectRow:row inComponent:1 animated:NO];
}

What i want is scrolling any components should scroll all together just like it is only one. It should not show even part of second for auto scrolling.

If it is possible, what do you recommend to do?...


Solution

  • Oke I got the solution with comment of Anupdas ...

    By giving number of spaces i get solution to my problem.

    -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
        __weak NSString *lbl1 = [NSString stringWithString:[[finalArray objectAtIndex:row] objectForKey:@"value1"]];
        lbl1 = [lbl1 stringByAppendingString:@"                                            "];
        lbl1 = [lbl1 stringByAppendingString:[[finalArray objectAtIndex:row] objectForKey:@"value2"]];
        return lbl1;
    }