UIPickerView在重新滚动之前不会重新加载

时间:2012-09-21 16:47:25

标签: objective-c ios

当我更改我的picker1时,它应该重新加载我的picker2,但只有当我滚动它时才会发生。我能做什么?谢谢!

    @implementation ViewController

    @synthesize picker1, array1, arrayApple, arraySamsung, picker2, array2;

    - (void)viewDidLoad
    {
        [super viewDidLoad];

        array1 = [[NSMutableArray alloc] initWithObjects:@"Apple", @"Samsung",nil];
        arrayApple = [[NSMutableArray alloc] initWithObjects: @"iPhone 5", @"iPad", @"iPod Touch", @"MacBook Pro", nil];
        arraySamsung = [[NSMutableArray alloc] initWithObjects: @"Galaxy Tab", @"Galaxy S3", @"Galaxy S", @"Galaxy 5", nil];

        self.array2 = self.arrayApple;

        [picker1 reloadAllComponents];
        [picker2 reloadAllComponents];

        // Do any additional setup after loading the view, typically from a nib.
    }

    - (void)viewDidUnload
    {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
    }



    //Picker View Methods

    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
        //NSLog(@"Selected Index: %@. Index of selected color: %i", [array1 objectAtIndex:row], row);

        if(pickerView == self.picker1) {
            if(row == 1) {
                [self.array2 removeAllObjects];
                self.array2 = self.arraySamsung;
                [picker2 reloadAllComponents];
                NSLog(@"%@", picker2);
            }
        }


    }


    - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
        return 1;
    }
    - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {



        if (pickerView == self.picker1) {
            return [array1 count];
        } else {
            return [array2 count];
        }

        return 0;
    }

    - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {

        if (pickerView == self.picker1) {
            return [self.array1 objectAtIndex:row];
        } else {
            return [self.array2 objectAtIndex:row];
        }

    }



    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    }

1 个答案:

答案 0 :(得分:0)

试试这个

if(row == 1) {
    self.array2 = self.arraySamsung;
} else {
    self.array2 = self.arrayApple;
}
    [picker2 reloadAllComponents];
    NSLog(@"%@", picker2);