IOS7 UIPickerView

来源:互联网 发布:手机淘宝在哪开店 编辑:程序博客网 时间:2024/06/03 16:34

  在头文件中饮用UIPickerView的两个代理 <UIPickerViewDelegate ,UIpickerViewDataSource>

    UIPickerView *pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(100, 250, 200, 100)];

    pickerView.delegate = self ;
    pickerView.dataSource = self ;
    [pickerView reloadAllComponents];

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