0

我在模态视图中的视图控制器中使用 UIPickerView(如果这就是它的名称)。我到处读到[picker reloadAllComponents];viewDidLoad 应该可以解决问题,但事实并非如此。我已经覆盖了-(UIView*)viewForRow-method,但我认为这不应该是问题..

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

我尝试reloadAllComponents同时输入viewDidLoadand viewWillAppear,但它不起作用。

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

1 回答 1

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;
}
于 2013-04-26T14:32:11.617 回答