UIPickerView的使用

来源:互联网 发布:安卓手机编程入门 编辑:程序博客网 时间:2024/05/17 07:38
一: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.pickerselectedRowInComponent:0];//获取选中的位置

                       [self.pickerselectRow:rowIndexinComponent:componentanimated: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=[[UIViewalloc]initWithFrame:CGRectMake(0,0,APPW,50)];

   UILabel *label=[[UILabelalloc]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=[cityListobjectAtIndex:row][@"name"];


        }elseif (component==1) {//第二列

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

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

        }

    }

    [view1addSubview: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 = [cityListobjectAtIndex:row];

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

            [pickerViewreloadComponent:1];

            

        }elseif(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
原创粉丝点击