码迷,mamicode.com
首页 > 其他好文 > 详细

UIPickVIew

时间:2015-04-17 15:45:24      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:uipickerview

//
//  CItyPickerViewController.m
//  CityPickViewController
//
//  Created by xiaoyao on 15/4/17.
//  Copyright (c) 2015年 lijien. All rights reserved.
//

#import "CItyPickerViewController.h"

@interface CItyPickerViewController () {
  NSDictionary *_cityDataPickerDict;
  NSArray *_provinceDataArray;
  NSArray *_cityDataArray;
  
  UIPickerView *_pickerView;
}

@end

@implementation CItyPickerViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  
  CGFloat x = 0;
  CGFloat y = 300;
  CGFloat w = [UIScreen mainScreen].bounds.size.width;
  CGFloat h = 200;
  
  _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(x, y, w, h)];
  _pickerView.backgroundColor = [UIColor lightGrayColor];
  [self.view addSubview:_pickerView];
  
  _pickerView.userInteractionEnabled = YES;
  
  _pickerView.delegate = self;
  _pickerView.dataSource = self;
  [self getData];
}


- (void)getData {
  NSString *pathPlist = [[NSBundle mainBundle] pathForResource:@"c" ofType:@"plist"];
  _cityDataPickerDict = [NSDictionary dictionaryWithContentsOfFile:pathPlist];
  
  _provinceDataArray = _cityDataPickerDict.allKeys;
  
  for (NSString * selectProvince in _provinceDataArray) {
     _cityDataArray = [_cityDataPickerDict objectForKey:selectProvince];
  }
}

- (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}

#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
  return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
  if (component == 0) {
    return _provinceDataArray.count;
  } else {
    return _cityDataArray.count;
  }
}

#pragma mark - UIPickerViewDelegate
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
  if (component == 0) {
    return [_provinceDataArray objectAtIndex:row];
  } else {
    return [_cityDataArray objectAtIndex:row];
  }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
  if (component == 0) {
    NSString *selectProvince = [_provinceDataArray objectAtIndex:row];
    _cityDataArray = [_cityDataPickerDict objectForKey:selectProvince];
    [_pickerView reloadComponent:1];
  }
}

//- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
//  return (component == 0 ? 90 : 80);
//}
@end

UIPickVIew

标签:uipickerview

原文地址:http://blog.csdn.net/u010606986/article/details/45096587

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!