ios-day10-03(省份和城市联动。UIPickerView的使用、使用UIPickerView的reloadComponent:方法刷新某一列)

源码下载地址:http://download.csdn.net/detail/liu537192/8515579


效果图:



核心代码:

//
//  JLViewController.m
//  03-省份城市联动
//
//  Created by XinYou on 15-3-10.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import "JLViewController.h"
#import "JLProvince.h"

@interface JLViewController ()
    
    
     
     
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
/**
 *  用于显示当前选中的省份和城市的label
 */
@property (weak, nonatomic) IBOutlet UILabel *provinceAndCityLabel;

@property (nonatomic, strong)NSArray *provinces;

@end

@implementation JLViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 设置数据源
    self.pickerView.dataSource = self;
    
    // 设置代理
    self.pickerView.delegate = self;
    
    // 主动调用代理方法,设置pickView默认选中第0列第0行
    [self pickerView:nil didSelectRow:0 inComponent:0];
}

- (NSArray *)provinces{

    if (_provinces == nil) {
        // 获取plist文件的路径
        NSString *path = [[NSBundle mainBundle] pathForResource:@"cities.plist" ofType:nil];
        
        // 加载plist文件的内容到数组
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
        
        NSMutableArray *tempArray = [NSMutableArray array];
        
        // 数组转模型
        for (NSDictionary *dict in dictArray) {
            JLProvince *province = [JLProvince provinceWithDict:dict];
            
            [tempArray addObject:province];
        }
        
        _provinces = tempArray;
    }
    
    return _provinces;
}

#pragma mark -数据源方法
/**
 *  pickerView共有多少列
 */
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{

    return 2;
}

/**
 *  pickerView第component列有多少行
 */
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{

    if (0 == component) {
        
        return self.provinces.count;
    }
    
    // 获得第0列当前选中的行号
    int zeroRow = [self.pickerView selectedRowInComponent:0];
    // 根据行号确定省份
    JLProvince *province = self.provinces[zeroRow];
    // 根据省份确定第1列城市的数量
    return province.cities.count;
    
}

#pragma mark -代理方法
/**
 *  pickerView第component列的第row行显示的字符串
 */
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{

    
    if (0 == component) {
        JLProvince *province = self.provinces[row];
        return province.name;
    }
    
    // 获得第0列当前选中的行号
    int zeroRow = [self.pickerView selectedRowInComponent:0];
    // 根据行号确定省份
    JLProvince *province = self.provinces[zeroRow];
    // 根据省份确定第1列要显示的城市
    return province.cities[row];
    
}

/**
 *  当选中了pickerView的第component列的第row行会调用这个方法
 */
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
    // 如果第0列有变化,就要刷新第1列的数据,并且让第1列选中第0行
    if (0 == component) {
        // 刷新第1列(因为第1列是根据第0列显示的省份来显示不同的城市)
        [self.pickerView reloadComponent:1];
        
        [self.pickerView selectRow:0 inComponent:1 animated:YES];
    }
    
    // 获得第0列当前选中的行号
    int zeroRow = [self.pickerView selectedRowInComponent:0];
    // 根据行号确定省份
    JLProvince *province = self.provinces[zeroRow];
    // 获取省份名
    NSString *provinceName = province.name;
    
    // 获得第1列当前选中的行号
    int firstRow = [self.pickerView selectedRowInComponent:1];
    // 根据行号获取城市名
    NSString *cityName = province.cities[firstRow];
    
    self.provinceAndCityLabel.text = [NSString stringWithFormat:@"%@--%@", provinceName, cityName];
}

@end

    
    
//
//  JLProvince.h
//  03-省份城市联动
//
//  Created by XinYou on 15-3-10.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import 
     
     
      
      

@interface JLProvince : NSObject
/**
 *  省份名称
 */
@property (nonatomic, copy)NSString *name;
/**
 *  该省中所有的城市
 */
@property (nonatomic, strong)NSArray *cities;

- (instancetype)initWithDict:(NSDictionary *)dict;

+ (instancetype)provinceWithDict:(NSDictionary *)dict;

@end
//
//  JLProvince.m
//  03-省份城市联动
//
//  Created by XinYou on 15-3-10.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import "JLProvince.h"

@implementation JLProvince

- (instancetype)initWithDict:(NSDictionary *)dict{

    if (self = [super init]) {
        [self setValuesForKeysWithDictionary:dict];
    }
    
    return self;
}

+ (instancetype)provinceWithDict:(NSDictionary *)dict{

    return [[self alloc] initWithDict:dict];
}

@end

     
     


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值