UIPickerView常用属性

一、UIPickerView的常用属性
UIPickerView的集成关系:UIPickerView:UIView:UIResponder:NSObject

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.citys = @[@"北京",@"西安"];
    self.beijingRegions = @[@"朝阳区",@"大兴区",@"丰台区",@"石景山区"];
    self.xianRegions = @[@"雁塔区",@"高新区",@"长安区",@"新城区"];
    self.selectedCity = 0;

    /**
     1、初始化、DateSource和Delegate

     */
    UIPickerView *pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(50, 100, 220, 200)];
    [self.view addSubview:pickerView];

    pickerView.delegate = self;
    pickerView.dataSource = self;



    /**
     2、获取pickerView的组件数

         .numberOfComponents        //获取分区数,只读属性
         - numberOfRowsInComponent: //获取某一分区的行数
         - rowSizeForComponent:     //获取某一分区行的尺寸
     */
    NSLog(@"%ld",(long)pickerView.numberOfComponents);
    NSLog(@"%ld",(long)[pickerView numberOfRowsInComponent:1]);
    NSLog(@"%f",[pickerView rowSizeForComponent:0].width);



    /**
     3、重载PickerView

         - reloadAllComponents      //重载所有分区
         - reloadComponent:         //重载某一分区
     */
    [pickerView reloadAllComponents];
    [pickerView reloadComponent:1];



    /**
     4、设置选中分区和行数、获取某分区选中的行

         - selectRow:inComponent:animated:  //设置选中某一分区某一行
         - selectedRowInComponent:          //返回某一分区选中的行
     */
    [pickerView selectRow:1 inComponent:0 animated:YES];
    [pickerView selectRow:2 inComponent:1 animated:YES];
    NSLog(@"%ld",(long)[pickerView selectedRowInComponent:0]);



    /**
     5、获取某一分区某一行的视图

         - viewForRow:forComponent: //必须实现代理中 pickerView:viewForRow:forComponent:reusingView:方法,否则返回nil
     */


    /**
     6、是否显示选择框,在iOS7之后这个属性没有任何效果
         .showsSelectionIndicator
     */
    pickerView.showsSelectionIndicator = YES;
}

UIPickerView常用属性_第1张图片

二、UIPickerView的DateSource(数据源)和Delegate(代理)

#pragma mark - UIPickerView DataSource(两个方法为必须要实现的方法)
//设置分区数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}
//根据分区设置行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (0 == component)
        return 2;
    else
        return 4;
}


#pragma makr - UIPickerView Delegate
//设置分区宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    return 100;
}
//设置分区行高
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    if (0 == component)
        return 50;
    else
        return 30;
}
//设置某一行显示的标题
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (0 == component)
    {
        return [self.citys objectAtIndex:row];
    }
    else
    {
        if (0 == self.selectedCity)
            return [self.beijingRegions objectAtIndex:row];
        else
            return [self.xianRegions objectAtIndex:row];
    }

}
//选中某一行时执行的回调
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (0 == component)
    {
        self.selectedCity = row;
        [pickerView reloadComponent:1];
    }
}

//通过属性字符串设置某一行显示的标题
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component

//设置某一行显示的view视图
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view

你可能感兴趣的:(iOS开发之UI控件)