我在模态视图中的视图控制器中使用 UIPickerView(如果这就是它的名称)。我到处读到[picker reloadAllComponents];
viewDidLoad 应该可以解决问题,但事实并非如此。我已经覆盖了-(UIView*)viewForRow
-method,但我认为这不应该是问题..
问题是,当 viewController 垂直弹出时,选择器是空的。在我向下滚动以便出现新行之前,它一直是空的。但是,如果我添加[picker reloadAllComponents];
到viewDidAppear
,则数据会在 viewController 完成它的介绍动画(垂直)后立即显示。不过这很烦人。我希望选择器在向上滑动时已经拥有它的数据。
我尝试reloadAllComponents
同时输入viewDidLoad
and 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;
}