dejoong
dejoong

Reputation: 2233

UIPickerView reloadAllComponents not working

I got problems with reload component in UIPickerView. Here is my code:

on my header file (.h):

@interface GoToAyatViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>{
    IBOutlet UIPickerView *pickerViewObj;
}
@property (nonatomic, retain) IBOutlet UIPickerView *pickerViewObj;

@end

on my implementation (.m):

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) { 
        // Custom initialization.
        ratusan = [[NSMutableArray alloc] initWithObjects:@"      0",@"      1",nil];
        puluhan = [[NSMutableArray alloc] initWithObjects:@"      0",@"      1",@"      2",
                @"      3",@"      4", @"      5", 
                @"      6", @"      7", @"      8", 
                @"      9",nil];
        satuan = [[NSMutableArray alloc] initWithObjects:@"      0",@"      1",@"      2",
                  @"      3",@"      4", @"      5", 
                  @"      6", @"      7", @"      8", 
                  @"      9",nil];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    pickerViewObj = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 250, 400, 80)];
    pickerViewObj.showsSelectionIndicator = YES;
    pickerViewObj.dataSource = self;
    pickerViewObj.delegate = self;  
    [self.pickerViewObj selectRow:2 inComponent:0 animated:NO];
    [pasalTypeButton setBackgroundColor:[UIColor blueColor]];
    self.isPasal = YES;
    self.title = self.kitab;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {

    return kPICKERCOLUMN;
    NSLog(@"numberOfComponentsInPickerView jalan");
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    NSLog(@"numberOfRowsInComponent jalan");
    if (component==0){
        return [ratusan count];
    }else if (component == 1) {
        return [puluhan count];
    }else{
        return [satuan count];
    }

}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSLog(@"set title run");
    switch (component) 
    {
        case 0:
            return [ratusan objectAtIndex:row];
            //break;
        case 1:
            return [puluhan objectAtIndex:row];
            //break;
        case 2:
            return [satuan objectAtIndex:row];
            //break;
    }
    return nil;
}

and in the implementation i put ibaction to set new value for 1st component on my uipickerview and fire reloadallcomponents:

-(IBAction) showAyat
{
self.ratusan = nil;
    self.ratusan = [[NSMutableArray alloc] initWithObjects:@"      0",@"      1",@"      2",nil];
    [self.pickerViewObj reloadAllComponents];
    NSLog(@"total ratusan=%d", [self.pickerViewObj numberOfRowsInComponent:0]);
}

here the picture of my nib setting in case i do wrong in setting delegate or datasource.

xib setting

by code above when i fire (tap button) showAyat method, it doesnt change the valu of my uipickerview at runtime, but i can see numberOfRowsInComponent:0 has already changed to 3 (before 2) using NSLog. I dont know what is wrong with my pickerview doesnt update the display. Please someone help me out here.

Upvotes: 1

Views: 3639

Answers (1)

Adil Soomro
Adil Soomro

Reputation: 37729

Problem:

You are creating two UIPickerView.

  • One in Xib (connecting as IBOutlet).
  • Second in code (inside viewDidLoad: method).

Both get data values from your delegate.

Since the picker created in Xib loses connection to the pickerViewObj variable when you initialize another second UIPickerView in code. So the UIPickerView presented at the time doesn't changes its value.

Solution:

Remove the code for creating UIPickerView from code.

Upvotes: 3

Related Questions