UIPickerView的使用

一:UIPickerView常用方法
           1>返回component列,row行的一个UIView,这里只有在定制的情况下才会使用,其他情况返回nil
             - (UIView *)viewForRow:(NSInteger)row forComponent:(NSInteger)component;
           2>重新装载整个UIPickerView所有列的数据和指定列的数据,及刷新所有数据和制定列数据
             - (void) reloadAlllComponents;
             - (void) reloadComponent:(NSInteger)component;
           3>现在UIPickerView中component列,row行,也就是让改行滚动到中央
             - (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated;
           4>返回定制列component中选中的行,没有选中返回-1;
             - (NSInteger)selectedRowInComponent:(NSInteger)component;
           5>.UIPickerView的常见属性
               // 数据源(用来告诉UIPickerView有多少列多少行)
              @property(nonatomic,assign) id<UIPickerViewDataSource> dataSource;
              // 代理(用来告诉UIPickerView每1列的每1行显示什么内容,监听UIPickerView的选择)
             @property(nonatomic,assign) id<UIPickerViewDelegate>   delegate; 
              // 是否要显示选中的指示器
             @property(nonatomic)  BOOL   showsSelectionIndicator;
             // 一共有多少列
             @property(nonatomic,readonly) NSInteger numberOfComponents;


二:UIPickerViewDataSource

        1、返回UIPickerView一共有几列

    - (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView;

       2、返回定制的component列有几行数据

    - (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component; 

三:UIPickerViewDelegate

        //给指定组件返回(设置)宽度,单位为像素
     - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component;
    //给指定组件返回(设置)行高,单位为像素 
    - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component;
    //显示UIPickerView的内容,只能是字符串
    - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
    // 对显示的字符串内容进行相应的样式设置
    - (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component 
    //给指定组件和行号返回相应位置应显示的自定义视图,优先级高于pickerView:titleForRow:forComponent
     - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;
    //选中某一列某一行
    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;

四:自定义UIPickerView实现文字颜色的改变.
   效果图:
        
   最主要思想是:1:自定义UIPickerView,实现对文字的操作.
              2:在滚动省份的时候,要及时刷新对应城市数据进行显示.并使用NSUserDefaults记录选中的位置,方便下次进来根据记录的位置显示自己选中的城市.
              3:关注两个方法:

                      NSInteger rowIndex=[self.picker selectedRowInComponent:0];//获取选中的位置

                        [ self . picker selectRow :rowIndex inComponent :component animated : NO ];通过该方法滚动到指定的位置.
部分代理方法代码如下:

#pragma mark----pickerViewDelegate---------

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView

{

    if (isSexPicker==YES) {//显示两种类型的UIPickerView

       return 1;

    }else{

       return 2;

    }

}

- (NSInteger)pickerView:(UIPickerView *)pickerView  numberOfRowsInComponent:(NSInteger)component

{

    

    if (isSexPicker==YES) {

        return sexs.count;

    }else{

        

        if (component==0) {

            return cityList.count;//省份

        }

        return  childCityList.count;//市的个数

    }

}

//自定义显示内容

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{

    

    UIView *view1=[[UIView alloc]initWithFrame:CGRectMake(0, 0,APPW,50)];

    UILabel *label=[[UILabel alloc]init];

    if (isSexPicker==YES) {

        label.frame=CGRectMake(APPW/2.0-10, 0,APPW,50);

        label.text=sexs[row];


    }else{

        if (component==0) {//第一列

            label.frame=CGRectMake(APPW*0.4, 0,APPW/2,50);

            label.text=[cityList objectAtIndex:row][@"name"];


        }else if (component==1) {//第二列

            label.frame=CGRectMake(100, 0,APPW/2,50);

            label.text=childCityList[row][@"name"];

        }

    }

    [view1 addSubview:label];

    return view1;

}

-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{

    return 50;

}

// 当用户选中UIPickerViewDataSource中指定列、指定列表项时激发该方法

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:

(NSInteger)row inComponent:(NSInteger)component

{

  

    if (isSexPicker==YES) {

        self.contentComponent=sexs[row];

        self.sexIndex=row;//记录位置

    }else{

        

        if (component==0) {

            self.oneComponent=cityList[row][@"name"];

            self.proviceIndex=row;//记录省份的位置位置

            

            //重点:根据选中省份的位置获取第二列显示的数据并刷新,这样才可以看到实时显示数据的效果

            NSDictionary *dic = [cityList objectAtIndex:row];

            childCityList =[NSArray arrayWithArray:[dic objectForKey:@"childrenList"]];//获取市城市名称

            [pickerView reloadComponent:1];

            

        }else if(component==1){

            self.twoComponent=childCityList[row][@"name"];

            self.mayorIndex=row;

        }

    }

}


- (CGFloat)pickerView:(UIPickerView *)pickerView

    widthForComponent:(NSInteger)component

{

    if (isSexPicker==NO) {

        

        return APPW/2.0;

    }

    return APPW;

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值