2011-06-20 57 views
0

所以我的UIPickerView没有连接到数据源。我不知道为什么。UIPickerView没有连接到数据源

我使用UIPickerViewDelegateUIPickerViewDataSource创建了一个文件,并正确地执行了在触摸textField时引入UIPickerView的过程。

pickerView的工作原理,但不显示任何组件。我实施了

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView 
{ 
return 3; 
} 

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component 



- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { 


- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { 


- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{ 

任何想法为什么pickerView没有连接到DataSource?我也试过所有这些方法的NSLogging。 // Does not print

我还设置

packSizePopPickerView.delegate = self; 
packSizePopPickerView.dataSource = self; 
packSizePopPickerView = [[UIPickerView alloc] init]; 
+0

你设置'delegate'和''你的UIPickerView'性能dataSource'? – albertamg

+0

你设置委托和数据源的方法是什么?在IB或代码中创建选择器视图?并且不要创建新帖子 - 如果您需要添加更多信息,请修改帖子。 – thomashw

+0

我在cellForRowAtIndexPath中设置它,因为我需要它出现在UITableViewCell中的textField ....事情是..我有另一个视图,它实现了SAME pickerView,它工作。为什么这不起作用! ? :( – Legolas

回答

1

在cellForRowAtIndexPath中,您将设置packSizePopPickerView的dataSource/delegate,然后将packSizePopPickerView分配/初始化为新的UIPickerView。 NEW UIPickerView不再具有其设置的委托/数据源。

这是错误的:

packSizePopPickerView.delegate = self; 
packSizePopPickerView.dataSource = self; 
packSizePopPickerView = [[UIPickerView alloc] init]; 

试试这个:

packSizePopPickerView = [[UIPickerView alloc] init]; 
packSizePopPickerView.delegate = self; 
packSizePopPickerView.dataSource = self; 
+0

我刚试过,仍然没有显示3个组件,而且我不认为你的回答是正确的,因为它不在你指定的位置 – Legolas

+0

你还没有回答我的问题 - 你是在IB还是在代码中设置这个pickerview?尝试通过cellForRowAtIndexPath中的[myPickerView reloadAllComponents]来重新加载你的pickerview,并且看看你的代理/数据源方法是否被调用。 – thomashw

+0

@Thomashw:我做它的代码。没有使用InterfaceBuilder,我尝试重新加载*仍然没有进展* – Legolas

0

你需要明确地告诉捡拾工具什么的数据源和代表们。

myPicker.delegate = someController; myPicker.datasource = someController;

+0

是的,我已经这样做了,检查更新的问题 – Legolas