组件要求:
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
时间: 2024-10-14 12:38:59