Search code examples
iosobjective-ccocoa-touchuipickerviewviewdidload

Load data in UIPickerView before it appears


I am using a UIPickerView in a viewcontroller that is in a modal view(if that's what it's called). I've read everywhere that [picker reloadAllComponents]; in viewDidLoad should do the trick, but it doesn't. I have overridden the -(UIView*)viewForRow-method, but I don't think that should be the problem..

The thing is, when the viewController pops up vertically, the picker is empty. And it stays empty until I scroll down so that a new row can appear. However, if I add [picker reloadAllComponents]; to viewDidAppear, then the data shows up immediately after the viewController has completed it's intro-animation(vertically). This is annoying though. I want the picker to already have it's data when it slides up.

I tried putting reloadAllComponents in both viewDidLoad and viewWillAppear, but it won't work.

I put an NSLog in the delegate method, and it prints out correct data, and it does it before the view pops up, but why doesn't it show it?

I have had the same problem with tableViewController before, and I think reloadData did the trick then, but why won't it draw my views in viewDidLoad? It only does it if it's called in viewDIDappear..

Code:

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if(component == TYPE1)
        return [[con getTypes] count];

    else return 1;

}
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UIView* newView = [[UIView alloc] init];

    //Create a label, put it on the left side of the view
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(view.frame.origin.x + 5, view.frame.origin.y, view.frame.size.width-45, view.frame.size.height)];
    [label setText:[[[con getTypes] objectAtIndex:row] getName]];
    [newView addSubview:label];//add

    //Create an imageview, put it on the right side of the view
    UIImageView*imageViewType = [[UIImageView alloc] initWithFrame:CGRectMake(view.frame.origin.x+(view.frame.size.width-40), view.frame.origin.y, 40, 40)];
    [imageViewType setImage:[[[con getTypes] objectAtIndex:row] getImage]];
    [newView addSubview:imageViewType];//add

    [label setBackgroundColor:[UIColor clearColor]];


    NSLog(@"%@", [[[con getTypes]objectAtIndex:row] getName]);
    //This NSLog prints out the correct name for the top three items of the list
    //-even when called from viewDidLoad, and even before the view appears, but
    //the content never shows up in the picker unless I scroll or call it again in 
    //viewDidAppear..

    return newView;
}

Solution

  • -(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
    {
        UIView* newView;
    
        if (view) 
           newView = view;
        else { 
           newView = [[UIView alloc] initWithFrame:CGRectMake(0,0,250,50)];  // use some appropriate size hier. I just made some figures up which almost certainly will not work. 
          //Create a label, put it on the left side of the view
          UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(5.0f, 0.0f, newView.frame.size.width-45, newView.frame.size.height)];
          [newView addSubview:label];//add
    
        //Create an imageview, put it on the right side of the view
          UIImageView*imageViewType = [[UIImageView alloc] initWithFrame:CGRectMake((newView.frame.size.width-40.0f), 0.0f, 40.0f, 40.0f)];
          [newView addSubview:imageViewType];//add
        }
    
        [label setText:[[[con getTypes] objectAtIndex:row] getName]];
        [label setBackgroundColor:[UIColor clearColor]];
    
        [imageViewType setImage:[[[con getTypes] objectAtIndex:row] getImage]];
    
        return newView;
    }