您的位置:首页 > 产品设计 > UI/UE

UIPickerView多列依赖列表互动示例

2017-01-02 20:25 274 查看
在storyboard中拖一个Picker View控件进来,属性保持默认。并给它定义一个变量pickerView1

头文件

//
//  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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息