PickerView - IOS

一、UIPickerView(拾取器)的使用

1、UIPickerView控件生成的表格可以提供滚动的轮盘,

2、这些表格表面上类似于标准的UITableView控件,但是他们使用的dataSource和delegate方法有细微的差别

二、UIPickerView常用方法

1、获取指定列的行数

 - (NSInteger)numberOfRowsInComponent:(NSInteger)component;

2、刷新所有列

 - (void)reloadAllComponents;

3、刷新指定的列

 - (void)reloadComponent:(NSInteger)component;

4、选择一行

 - (void)selectRow:(NSInteger)row inComponent:(NSInteger component
animated:(BOOL)animated;

5、获取指定列当前显示的是第几行

 - (NSInteger)selectedRowInComponent:(NSInteger)component;

三、UIPickerView常用方法

 1、dataSource方法

   1)返回列数

     - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

   2)返回每一列对应的行数

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

 2、delegate方法

    1)返回显示的文本

     - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

    2)当某一列选中的行数发生变化时调用

      - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

    3) 设置行高

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

    4)设置列宽

     - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component 

    5)自定义cell

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

四、练习

1、使用拾取器做城市选择

   1)在工程自带的ViewController的viewDidload加入

     UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,self.view.frame.size.height-300, 414, 300)];
     pickerView.backgroundColor = [UIColor grayColor];    
     [self.view addSubview:pickerView];


   运行,什么都没有,因为多少段多少行是通过代理方法来设置的


   2)让viewController类遵守  两个协议,在viewDidLoad方法里给pickerView挂上代理,并在viewController里实现如下两个代理方法

     //返回列数
     - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {

        return 2;
     }

     //返回每一列中的行数
     - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {

       return 5;

     }


   运行 选择器里面有两列,每列的数据都是用『?』表示,因为还没有给每列赋值


   3)将课件里面的city.plist文件拖到工程中,在viewDidLoad方法里写入如下代码

      NSString *path = [[NSBundle mainBundle] pathForResource:@"city" ofType:@"plist"];

      //声明一个全局变量dataArray
      dataArray = [[NSArray alloc] initWithContentsOfFile:path];

   4)将代理方法- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component的内容替换成如下内容

          if (component == 0) {
    
            return data.count;
          }

          NSInteger selectedRow =  [pickerView selectedRowInComponent:component];

          NSArray *tempArray = data[selectedRow][@"cities"];

          return tempArray.count;


   运行 目前还是没有数据,但列数和行数已经做了自动设置



   5、给每行设置标题,将如下代理方法写入工程

     //返回每个item中的title
     - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {

        if (component == 0) {//第一列   
           NSDictionary *dic = data[row];
           NSString *state = dic[@"state"];
           return state;
        }

        //返回第一列选择的行的索引
        NSInteger selectedRow = [pickerView selectedRowInComponent:0];

        //取得省级字典
        NSDictionary *items = data[selectedRow];

        //取得该省下所有的市
        NSArray *cities = [items objectForKey:@"cities"];
        NSString *cityName = cities[row];
        return cityName;

     }

   运行 每行有标题 但左边的省份变化,右边的城市列表没有跟着变化


   6、实现右侧跟着左侧联动

      //当某一列选中的行数发生变化时调用
      - (void)pickerView:(UIPickerView *)pickerView
      didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

        if (component == 0) {
        //刷新指定列中的行
        [pickerView reloadComponent:1];
        //选择指定的item
        [pickerView selectRow:0 inComponent:1 animated:YES];
        }

      }

    运行 右侧可以跟着左侧联动


   7、设置列宽和行高,在工程里添加如下代理方法

      
      - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
        
        if (component == 0) {
         return 100;
        }
        return 220;
      }

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

        return 30;
      }


   8、自定义cell,添加代理方法

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

        if (component == 0) {
    
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        view.backgroundColor = [UIColor greenColor];
        return view;
        }

        UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 220, 40)];
        view2.backgroundColor = [UIColor redColor];

        return view2;
      }

      运行 只剩色块,而里面却没有内容了,因为这个代理方法执行- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component代理方法将不会再执行

你可能感兴趣的:(PickerView - IOS)