Iphone 如何为该选择器视图提供数据源
在这里,我以编程方式添加了一个pickerviewIphone 如何为该选择器视图提供数据源,iphone,ios,sdk,uipickerview,subview,Iphone,Ios,Sdk,Uipickerview,Subview,在这里,我以编程方式添加了一个pickerview - (void)viewDidLoad { [super viewDidLoad]; CGRect pickerFrame = CGRectMake(0,280,321,200); UIPickerView *myPickerView = [[UIPickerView alloc] init]; //Not sure if this is the proper allocation/initi
- (void)viewDidLoad {
[super viewDidLoad];
CGRect pickerFrame = CGRectMake(0,280,321,200);
UIPickerView *myPickerView = [[UIPickerView alloc] init]; //Not sure if this is the proper allocation/initialization procedure
//Set up the picker view as you need it
//Set up the display frame
myPickerView.frame = pickerFrame; //I recommend using IB just to get the proper width/height dimensions
//Add the picker to the view
[self.view addSubview:myPickerView];
}
但现在我需要让它显示内容,并以某种方式找出它何时改变,以及改变了什么值。我该怎么做 您需要创建一个实现
UIPickerViewDataSource
协议的类,并将其实例分配给myPickerView.dataSource
在.h文件中放置此代码
@interface RootVC : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>
使用以下委托和datasouce方法
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
return 200;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return 50;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *returnStr = @"";
if (pickerView == myPickerView)
{
returnStr = [[levelPickerViewArray objectAtIndex:row] objectForKey:@"nodeContent"];
}
return returnStr;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (pickerView == myPickerView)
{
return [levelPickerViewArray count];
}
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
在xcode文档中查看UIPickerView数据源和委托。如何分配实例?@Michael您只需设置myPickerView.datasource=(数据源的实例)。数据源实例的来源特定于您的应用程序。请记住,UIPickerView不保留其数据源,因此您需要在视图控制器中创建/保留它(如果不是在其他地方)。
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
return 200;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return 50;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *returnStr = @"";
if (pickerView == myPickerView)
{
returnStr = [[levelPickerViewArray objectAtIndex:row] objectForKey:@"nodeContent"];
}
return returnStr;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (pickerView == myPickerView)
{
return [levelPickerViewArray count];
}
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}