gpt4 book ai didi

ios - 在 UIPickerView 出现之前加载数据

转载 作者:行者123 更新时间:2023-11-29 03:58:09 26 4
gpt4 key购买 nike

我在模态视图中的 View Controller 中使用UIPickerView(如果这就是它的名字)。我到处都读到 viewDidLoad 中的 [picker reloadAllComponents]; 应该可以解决问题,但事实并非如此。我已经重写了 -(UIView*)viewForRow-方法,但我认为这不应该是问题所在..

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

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

我在委托(delegate)方法中放置了一个NSLog,它打印出了正确的数据,并且在 View 弹出之前就完成了,但为什么它不显示它?

我之前在 tableViewController 上也遇到过同样的问题,我想 reloadData 就解决了这个问题,但是为什么它不会在 viewDidLoad 中绘制我的 View 呢?仅当在 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;
}

最佳答案

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

关于ios - 在 UIPickerView 出现之前加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16234716/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com