在UIPickerView出现之前加载数据

时间:2013-04-26 10:53:01

标签: ios objective-c cocoa-touch uipickerview viewdidload

我在一个处于模态视图中的viewcontroller中使用UIPickerView(如果这是它的名字)。 我已经读过viewDidLoad中[picker reloadAllComponents];应该做的所有地方,但事实并非如此。我已经覆盖了-(UIView*)viewForRow - 方法,但我认为这不应该是问题..

问题是,当viewController垂直弹出时,选择器为空。它向下滚动,直到我向下滚动,以便可以出现一个新行。但是,如果我将[picker reloadAllComponents];添加到viewDidAppear,则在viewController完成它的前奏动画(垂直)后立即显示数据。这很烦人。我希望选择器在滑动时已经拥有它的数据。

我尝试将reloadAllComponents放在viewDidLoadviewWillAppear中,但它不起作用。

我在委托方法中放了一个NSLog,它打印出正确的数据,它会在视图弹出之前完成,但为什么不显示呢?

之前我遇到过tableViewController的同样问题,我认为reloadData做了诀窍,但为什么不在viewDidLoad中绘制我的视图?只有在viewDIDappear中调用它才会这样做。

代码:

-(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;
}

1 个答案:

答案 0 :(得分:1)

-(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;
}