省份、城市选择组件

组件要求:

  1、能够选择中国的省份、城市

  2、组件具有可扩展性,较好的复用性

效果:

    

具体实施:

  1、类似于照片选择组件,第一个界面显示省份,第二个组件显示城市。

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//
//  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
复制代码

 

  

posted @   兜兜有糖的博客  阅读(2868)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示