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的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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值