• 【实战】如何实现各种拾取器


    wps_clip_image-20916

     UIPickerViewDelegate协议必须实现的方法有:

    pickerView: titleForRow:forComponent,这个方法根据指定的行号返回该行的标题,也就是向用户显示的字符串。

    UIPickerViewDataSource协议必须实现的方法有:

    1.numberOfComponentsInPickerView,这个方法返回UIPickerView需要多少个组件。

    2.pickerView: numberOfRowsInComponent,这个方法返回指定组件包含多少行。

    @interface SinglePickerViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> {
      
        NSArray    *pickerData;
    }
    @property (nonatomic,retain) IBOutlet UIPickerView *pickerView;

    #pragma mark--委托协议方法

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

                      return [pickerData objectAtIndex:row];

    }

    #pragma mark--数据源协议方法

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

    return 1;

    }

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

    return [pickerData count];

    }

    -(void)viewDidLoad{

        NSArray   *array = [[NSArray  alloc] initWithObjects:@"欧洲",@"非洲",nil];

    }

    -(IBAciton)onClickButton:(id)sender{

        NSInteger row = [pickerView selectedRowInComponent:0]; //获得第一列的选择中的行号。

        NSString *selected = [array ObjectAtIndex:row];

    }

    wps_clip_image-24785

    (非关联)

    @interface DoublePickerViewController:UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>{

             NSArray *array1;

             NSArray *array2;

    }

    @property (nonatomic,retain) IBOutlet *pickerView;

    #pragma mark--委托协议方法

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

                      if(component == 0){

                          return [array1 objectAtIndex:row];

                      } else {

                          return [array2 objectAtIndex:row];

                      }

    }

    #pargam mark--数据源协议方法

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

    return 2;

    }   

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

    if(component == 0){

    return [array1 count];

    } else {

    return [array2 count];

    }

    - (void)viewDidLoad {
        NSArray
    *array1 = [[NSArray alloc] initWithObjects:@"欧洲",
                          
    @"南美", @"非洲", @"北美",
                          
    @"亚洲", @"大洋洲", nil];
        
    NSArray *array2 = [[NSArray alloc] initWithObjects:@"足球",
                          
    @"篮球", @"羽毛球", @"乒乓球", nil];
        }

    -(IBAction)onClick:(id)sender{

        NSInteger row1 = [pickerView selectedRowInComponent:0];

        NSInteger row2 = [pickerView selectedRowInComponent:1];

        NSString *str1 = [array1 ObjectAtIndex:row1];

        NSString *str2 = [array2 ObjectAtIndex:row2];

        NSString *title = [[NSString alloc] initWithFormat:@"你选择了%@的%@项目!",str1,str2];

        UIAlertView *alert =[[UIAlertView alloc]initWithTitle:title message:@"谢谢你的选择."
                                            delegate:nil 
                                            cancelButtonTitle:
    @"Ok" 
                                            otherButtonTitles:nil];

        [alert show];

    }

     

    (关联)

    @interface DependentPickerViewController : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>{

        NSDictionary *data;
        NSArray
    *array1;
        NSArray
    *array2;

    }

    @property (nonatomic,retain) IBOutlet UIPickerView *pickerView;

     

    -(void) viewDidLoad{

       //bundle是一个目录,其中包含了程序会使用到的资源.

       NSBundle *bundle = [NSBundle mainBundle];

       NSString *plistPath = [bundle pathForResource:@"足球队dictionary" ofType:@"plist"];

       NSDictionary *data = [[NSDictionary alloc]initWithContentsOfFile:plistPath];

       //这几行代码是从statedictionary.plist属性列表文件中读取到NSDictionary对象中。

      NSArray *col1= [data allkeys];

      NSArray *array1 = [col1 sortedArrayUsingSelector:@selector(compare:)]; //对数据排序。

      NSString *selectCol1 = [array1 objectAtIndex:0];
      NSArray *array2 = [data objectForKey:selectCol1];

    }

       #pragma mark--委托协议方法
    - (NSString *)pickerView:(UIPickerView *)pickerView
                 titleForRow:(NSInteger)row forComponent:(NSInteger)component {
       
    if (component == 0) { //选择了第一列
            return [array1 objectAtIndex:row];
        }
    else {//选择了第二列
            return [array2 objectAtIndex:row];
        }
    }

    // 拖动左边的齿轮时,右边的数据相应的Reload更新。

    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row    //委托方法是实现拾取器控件两个轮互动关键
           inComponent:(NSInteger)component {
       
    if (component == 0) {
            NSString
    *selectCol1 = [pickerData1 objectAtIndex:row];
            pickerData2
    = [data objectForKey:selectCol1]; 
           
    [pickerView selectRow:0 inComponent:1 animated:YES];
            [pickerView reloadComponent:1];    //重新加载拾取器
        }

     }   

        pickerArray = [NSArray arrayWithObjects:@"动物",@"植物",@"石头", nil]; 

        dicPicker = [NSDictionary dictionaryWithObjectsAndKeys: 

        [NSArray arrayWithObjects:@"鱼",@"鸟",@"虫子",nil], @"动物"

        [NSArray arrayWithObjects:@"花",@"草",@"葵花",nil], @"植物"

        [NSArray arrayWithObjects:@"疯狂的石头",@"花岗岩",@"鹅卵石", nil], @"石头",nil]; 

  • 相关阅读:
    转载:CODE CSDN Git 配制方法介绍
    版本管理之Git(二):Win7上Git安装及简单配置过程
    Html_color code表示
    Android 自动化测试—robotium(十一) robotium实现微博绑定
    转载:Android自动化测试- 自动获取短信验证码
    Android 自动化测试—robotium(九) Junit_report测试报告重定向输出到终端SDCard
    每日一问:面试结束时面试官问"你有什么问题需要问我呢",该如何回答?
    常见的几个Python面试题
    使用python解析Json字符串-获取Json字符串关键字
    Android测试之 APK重签名方法
  • 原文地址:https://www.cnblogs.com/ejllen/p/3723228.html
Copyright © 2020-2023  润新知