UIPickerView多列依赖列表互动示例

前端之家收集整理的这篇文章主要介绍了UIPickerView多列依赖列表互动示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

在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

猜你在找的设计模式相关文章