在头文件中饮用UIPickerView的两个代理 <UIPickerViewDelegate ,UIpickerViewDataSource>
UIPickerView *pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(100, 250, 200, 100)];
pickerView.delegate = self ;[self.view addSubview:pickerView];
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSLog(@"row = %d component = %d" ,row ,component);
}
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
if (!view)
{
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 60, 30)];
label.text = [NSString stringWithFormat:@"row%d",row];
label.textColor = [UIColor blueColor];
view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 70, 30)];
view.backgroundColor = [UIColor greenColor];
[view addSubview:label];
}
return view ;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return 3;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 3;
}
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return 20.0f;
}