UIPickerView多列依赖列表互动示例
2017-01-02 20:25
274 查看
在storyboard中拖一个Picker View控件进来,属性保持默认。并给它定义一个变量pickerView1
头文件
实现文件
示例下载地址:http://download.csdn.net/detail/deng0zhaotai/9724310
头文件
// // PickerViewController.h // UIViewDemo // // Created by dcr on 2016/12/27. // Copyright © 2016年. All rights reserved. // #import <UIKit/UIKit.h> @interface PickerViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate> @end
实现文件
// // PickerViewController.m // UIViewDemo // // Created by dcr on 2016/12/27. // Copyright © 2016年. All rights reserved. // #import "PickerViewController.h" @interface PickerViewController () @property (weak, nonatomic) IBOutlet UIPickerView *pickerView1; @property (strong, nonatomic) NSArray *province; @property (strong, nonatomic) NSArray *guangdong; @property (strong, nonatomic) NSArray *jiangxi; @property (strong, nonatomic) NSArray *guangxi; @property (strong, nonatomic) NSArray *hunan; @property (strong, nonatomic) NSDictionary *citys; @property (strong, nonatomic) NSString *selectIndex; @end @implementation PickerViewController //设置列数 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return 2; } //设置每列行数 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ if(component == 0){ return _province.count; }else{ return [[_citys objectForKey:_selectIndex] count]; } } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. _province = [[NSArray alloc] initWithObjects:@"广东省", @"江西省", @"广西省", @"湖南省", nil]; _guangdong = [[NSArray alloc] initWithObjects:@"茂名市", @"广州市", @"深圳市", @"惠州市", @"东莞市", @"中山市", nil]; _jiangxi = [[NSArray alloc] initWithObjects:@"南昌市", @"九江市", @"吉安市", @"赣州市", nil]; _guangxi = [[NSArray alloc] initWithObjects:@"桂林市", @"梧州市", @"北海市", @"南宁市", nil]; _hunan = [[NSArray alloc] initWithObjects:@"长沙市", @"常德市", @"株洲市", @"湘潭市", @"邵阳市", @"娄底市", nil]; _citys = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:_guangdong, _jiangxi, _guangxi, _hunan, nil] forKeys:[NSArray arrayWithObjects:@"广东省", @"江西省", @"广西省", @"湖南省", nil]]; _selectIndex = @"广东省"; //设置协议代理 _pickerView1.delegate = self; _pickerView1.dataSource = self; } //显示值 - (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ if(component == 0){ return [_province objectAtIndex:row]; }else{ return [[_citys objectForKey:_selectIndex] objectAtIndex:row]; } } //选择事件 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ if(component == 0){ NSLog(@"_province = %@", [_province objectAtIndex:row]); _selectIndex = [_province objectAtIndex:row]; //加载第二列数据 [_pickerView1 reloadComponent:1]; //恢复第二列第一个 [_pickerView1 selectRow:0 inComponent:1 animated:true]; }else{ NSLog(@"_city = %@", [[_citys objectForKey:_selectIndex] objectAtIndex:row]); } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end实现效果
示例下载地址:http://download.csdn.net/detail/deng0zhaotai/9724310
相关文章推荐
- [XCode] XCode 6 中 UIPickerView 的简单示例
- iOS学习笔记-037.UITableView示例一——qq列表
- 两个UIPickerView控件间的数据依赖
- (UIPickerView)选择器的使用--相互依赖的多列选择器
- 两个UIPickerView控件间的数据依赖
- [ --> C Language<-- ] employee信息添加,删除,列表的示例程序
- 使用Autumn框架进行C++依赖注入示例
- SAP的SYSTEM的函数列表和代码示例
- 水晶报表设计概念的官方示例报表列表
- ABAP--SAP的SYSTEM的函数列表和代码示例
- ABAP--SAP的SYSTEM的函数列表和代码示例
- 关于SAP的TABLEVIEW编程的几个重要变量值的变化列表
- 免费使用Anole示例——动态创建带有排序功能和下拉列表风格的组合框
- C++--MFC的关键类(View,Application,Frame,Document等等)之间访问方法列表
- C++--MFC的关键类(View,Application,Frame,Document等等)之间访问方法列表
- ABAP--SAP的SYSTEM的函数列表和代码示例
- 下拉列表联动总结(AJAX下拉列表联动简单示例)
- C++--MFC的关键类(View,Application,Frame,Document等等)之间访问方法列表
- ABAP--SAP的SYSTEM的函数列表和代码示例
- 关于SAP的TABLEVIEW编程的几个重要变量值的变化列表