IOS pickerView 使用

来源:互联网 发布:linux查看所有进程命令 编辑:程序博客网 时间:2024/04/28 11:22

 源码下载

选择器,如同html select 一个pickerView可以实现多个select 关键是由numberOfComponentsInPickerView 这个决定有多少个列


400661.png

实现委托和数据源。



394345.png


300305.png


实现下面三个方法


// returns the number of 'columns' to display. //总共有多少列

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

    return 2;

    

}


// returns the # of rows in each component..   //列的数据

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

    if(component==0){

        return [array1 count];

    }else{;

        return [array2 count];

    }

}

//设置当前内容


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

    if(component==0){

        return [array1 objectAtIndex:row];

    }else{

        return [array2 objectAtIndex:row];

    }

  


}

实现效果:

855709.png

获取选择到的数据

 

39598.png


联动效果



921044.png


需要定义一个字典类型。相当于java Map类型


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    NSLog(@"DDDDD");

    mainArray = [NSArray arrayWithObjects:@"test1",@"test2",nil];

    myds = [NSDictionary dictionaryWithObjectsAndKeys:[NSArray arrayWithObjects:@"test11",@"test12"nil],@"test1",

                                                      [NSArray arrayWithObjects:@"test21",@"test22"nil],@"test2"nil];

    subArray = [myds objectForKey:@"test1"];

    _pick4.delegate=self;

    _pick4.dataSource=self;

    

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

// returns the number of 'columns' to display.

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

    return 2;

    

}


// returns the # of rows in each component..

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

    if(component==0){

        return [mainArray count];

    }else{;

        return [subArray count];

    }

}


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

    if(component==0){

        return [mainArray objectAtIndex:row];

    }else{

        return [subArray objectAtIndex:row];

    }

    

}

//选择相应列表的时候

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

    if(component==0){

        subArray  = [myds objectForKey:[mainArray objectAtIndex:row]];

        [pickerView selectRow:0 inComponent:1 animated:YES];

        [pickerView reloadComponent:1 ];

    }

}

//实现读取内容

- (IBAction)button4click:(id)sender {

    _text4.text = @"DDDD";

    NSInteger ar1 = [_pick4 selectedRowInComponent:0];

    NSInteger ar2 = [_pick4 selectedRowInComponent:1];

    NSString *str1 = [mainArray objectAtIndex:ar1];

    NSString *str2 = [subArray objectAtIndex:ar2];

    NSString *message  = [ str1  stringByAppendingFormat:@"---%@",str2  ];

    _text4.text = message;

 

    


}




114056.png





0 0
原创粉丝点击