UIPickerView城市選擇小Demo

重在UIPickerView的DataSourceDelegate和Delegate方法
atom

效果圖:spa

原代碼以下:.net

#define kScreenWidth     [UIScreen mainScreen].bounds.size.width3d

#define kScreenHeight     [UIScreen mainScreen].bounds.size.heightcomponent

#import "ViewController.h"orm


@interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>ci

@property (nonatomic , strong) UIPickerView *pickerView;get

@property (nonatomic , strong) NSArray *provinceArr;it

@property (nonatomic , strong) NSDictionary *cityDic;io

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    [self addPickerView];

}


-(void) addPickerView

{

    [self.view addSubview:self.pickerView];

}



#pragma mark UIPivkerView DataSource Delegate

- (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView

{

    return 2;

}


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

{

    if (component == 0) {

        return self.provinceArr.count;

    }

    else {

        NSInteger proviceRow = [pickerView selectedRowInComponent:0];

        NSString *provincName = self.provinceArr[proviceRow];

        NSArray *cityArr = self.cityDic[provincName];

        return cityArr.count;

    }

}


#pragma mark UIPickerView Delegate

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

{

    if(component == 0){

        [pickerView reloadComponent:1];

    }

    if(component == 1){

    NSInteger rowOne = [pickerView selectedRowInComponent:0];

    NSInteger rowTow = [pickerView selectedRowInComponent:1];

    NSString *provinceName = self.provinceArr[rowOne];

    NSArray *citys = self.cityDic[provinceName];

    NSString *cityName = citys[rowTow];

    NSLog(@"%@,%@",provinceName,cityName);

}

}


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

{

    if(component == 0){

        return self.provinceArr[row];

    }

    if(component == 1){

        NSInteger rowOne = [pickerView selectedRowInComponent:0];

        NSString *provinceName = self.provinceArr[rowOne];

        NSArray *citys = self.cityDic[provinceName];

        NSString *cityName = citys[row];

        return cityName;

    }

    return nil;


}

#pragma mark getter

- (UIPickerView *) pickerView

{

    if (!_pickerView) {

        _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, kScreenHeight-300, kScreenWidth, 300)];

        _pickerView.dataSource = self;

        _pickerView.delegate = self;

    }

    return _pickerView;

}


- (NSArray *) provinceArr

{

    if (!_provinceArr) {

        _provinceArr = @[@"北京", @"廣西", @"廣東",@"湖南"];

    }

        return _provinceArr;

}


- (NSDictionary *) cityDic

{

    if (!_cityDic) {

        _cityDic = @{

                  @"北京":@[@"朝陽區", @"東城區", @"西城區"],

                  @"廣西":@[@"桂林市", @"南寧市"],

                  @"廣東":@[@"惠州市", @"廣州市", @"深圳市",@"東莞市"],

                  @"湖南":@[@"長沙市",@"湘潭市",@"株洲市",@"常德市"]

                  };

    }

    return _cityDic;

}


此Demo在於理解UIPickerView,對於要實現城市地區功能選擇的小夥伴可創建一個.plist文件,在文件中添加省份和城市並一一對應。

後續將發表實現其功能的代碼😄。

相關文章
相關標籤/搜索