首页 > 代码库 > 省份、城市选择组件
省份、城市选择组件
组件要求:
1、能够选择中国的省份、城市
2、组件具有可扩展性,较好的复用性
效果:
具体实施:
1、类似于照片选择组件,第一个界面显示省份,第二个组件显示城市。
//// ViewController.m// CityPicker//// Created by vousaimer on 15-1-23.// Copyright (c) 2015年 va. All rights reserved.//#import "ViewController.h"#import "ProvinceViewController.h"@interface ViewController ()<CityPickerProtocol>@property (nonatomic, strong) UIButton *testButton;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; _testButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 45)]; [_testButton setTitle:@"cityPicker" forState:UIControlStateNormal]; _testButton.backgroundColor = [UIColor greenColor]; [self.view addSubview:_testButton]; _testButton.center = self.view.center; [_testButton addTarget:self action:@selector(testCityPicker:) forControlEvents:UIControlEventTouchUpInside]; }- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}- (void)testCityPicker:(id)sender{ ProvinceViewController *vc = [[ProvinceViewController alloc] initWithNibName:nil bundle:nil]; vc.delegate = self; [self presentViewController:[[UINavigationController alloc] initWithRootViewController:vc] animated:YES completion:^{ }];}- (void)CityPickerDidCancel:(ProvinceViewController *)provinceVC{ [provinceVC dismissViewControllerAnimated:YES completion:^{ }];}- (void)CityPickerDidChoose:(ProvinceViewController *)provinceVC withResultDic:(NSDictionary *)dic{ [provinceVC dismissViewControllerAnimated:YES completion:^{ NSString *province = dic[@"Province"]; NSString *city = dic[@"City"]; NSLog(@"province = %@ , city = %@",province, city); }];}@end
第二个组件显示城市
//// CityViewController.m// CityPicker//// Created by vousaimer on 15-1-24.// Copyright (c) 2015年 va. All rights reserved.//#import "CityViewController.h"@interface CityViewController ()@end@implementation CityViewController- (void)viewDidLoad { [super viewDidLoad]; self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}- (void)setCityArray:(NSArray *)cityArray{ _cityArray = cityArray; [[NSOperationQueue mainQueue] addOperationWithBlock:^{ [self.tableView reloadData]; }];}#pragma mark - Table view data source- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ // Return the number of sections. return _cityArray.count;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CityCell"]; if(cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ProvinceCell"]; } NSDictionary *dic = self.cityArray[indexPath.row]; cell.textLabel.text = dic[@"name"]; cell.accessoryType = UITableViewCellAccessoryNone; return cell;}- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [tableView deselectRowAtIndexPath:indexPath animated:YES]; if([self.delegate respondsToSelector:@selector(CityPickerDidChoose:withResultDic:)]) { NSArray *vcArray = self.navigationController.viewControllers; [self.delegate CityPickerDidChoose:vcArray[vcArray.count -2] withResultDic:@{@"City":self.cityArray[indexPath.row][@"name"], @"Province":self.Province}]; }}@end
省份、城市选择组件
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。