我在一個模式視圖中的視圖控制器中使用UIPickerView(如果這就是它的名稱)。 我讀過的所有地方[picker reloadAllComponents];
在viewDidLoad應該做的伎倆,但它沒有。我已經覆蓋-(UIView*)viewForRow
-方法,但我不認爲這應該是問題..在UIPickerView中加載數據之前,它出現
事情是,當viewController垂直彈出,選擇器是空的。直到我向下滾動,才能出現新的一行。但是,如果我將[picker reloadAllComponents];
添加到viewDidAppear
,那麼在viewController完成其介入動畫(垂直)後立即顯示數據。儘管這很煩人。我希望選取器在滑動時已經擁有數據。
我試着把reloadAllComponents
都放在viewDidLoad
和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;
}
嘗試awakefromnib – amar 2013-04-26 10:56:35
你在裝入UIPicker,NSMutableArray裏?如果是這樣,項目何時被添加到數組? – JDx 2013-04-26 10:59:51
@JDx帶有一些對象的NSMutableArray。我以編程方式創建這個viewController並使用'initWithController:(Controller *)c'(自制),並且控制器已經包含可變數組。在我的init方法中,我使用'self = [storyboard instantiateViewControllerWithIdentifier:@「identifier」];'然後設置數據,是這個問題嗎?..我不這麼認爲..當我調用'reloadAllComponents'時'viewDidLoad'它打印出正確的文本(文本作爲選擇器應該顯示),但它不會顯示它,直到我向下滾動代表踢入什麼或什麼.. – Sti 2013-04-26 11:05:36