ios多视图之间实现省市级的二级查找

为大家分享一下在ios中多视图实现省市级的查找,例如在上一个页面中点了“北京市”,会调到下一个页面显示“朝阳区,东城区,西城区”等。

 

首先新建一个工程,工程名自定,然后在创建两个视图FirstTableViewController和SecondTableViewController继承于UITableViewController

1.把省市表的 plist 文件导入到工程里面。

2.在AppDelegate.h文件里导入头文件FirstTableViewController.h。

3.在AppDelegate.m文件中实现如下代码:

 1 #import "AppDelegate.h"
 2 
 3 @interface AppDelegate ()
 4 
 5 @end
 6 
 7 @implementation AppDelegate
 8 
 9 
10 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
11     self.window.rootViewController=[[UINavigationController alloc] initWithRootViewController:[[FirstTableViewController alloc] initWithStyle:UITableViewStylePlain]];
12     return YES;
13 }

4.FirstTableViewController.h中定义一个集合属性

1 #import <UIKit/UIKit.h>
2 #import "SecondTableViewController.h"
3 @interface FirstTableViewController : UITableViewController
4 @property(strong,nonatomic) NSArray *array;
5 
6 @end

5.FirstTableViewController.h中实现如下代码

 1 #import "FirstTableViewController.h"
 2 
 3 @interface FirstTableViewController ()
 4 
 5 @end
 6 
 7 @implementation FirstTableViewController
 8 
 9 - (void)viewDidLoad {
10     [super viewDidLoad];
11     self.title=@"省级";
12     //设置导航栏的前景色
13     self.navigationController.navigationBar.titleTextAttributes=@{NSForegroundColorAttributeName:[UIColor redColor]};
14     UIBarButtonItem *item=[[UIBarButtonItem alloc] initWithTitle:@"下一页" style:2 target:self action:@selector(nextPage)];
15     self.navigationItem.rightBarButtonItem=item;
16     
17     //读取文件
18     NSString *path=[[NSBundle mainBundle]pathForResource:@"city" ofType:@"plist"];
19     self.array=[NSArray arrayWithContentsOfFile:path];
20 
21      //重用单元格的唯一标示
22     [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"myCell"];
23 }
24 -(void)nextPage
25 {
26     //跳转到下一页
27     SecondTableViewController *secondVc=[SecondTableViewController new];
28     [self.navigationController pushViewController:secondVc animated:YES];
29     
30 }
31 
32 - (void)didReceiveMemoryWarning {
33     [super didReceiveMemoryWarning];
34     // Dispose of any resources that can be recreated.
35 }
36 
37 #pragma mark - Table view data source
38 
39 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
40 //#warning Incomplete implementation, return the number of sections
41     return 1;
42 }
43 
44 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
45 //#warning Incomplete implementation, return the number of rows
46     return self.array.count;
47 }
48 
49 
50 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
51     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell" forIndexPath:indexPath];
52     
53     NSDictionary *tempDic=self.array[indexPath.row];
54     cell.textLabel.text=tempDic[@"State"];
55     cell.accessoryType=1;
56     return cell;
57 }
58 
59 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
60 {
61 
62     
63     SecondTableViewController *secondVc=[SecondTableViewController new];
64     [self.navigationController pushViewController:secondVc animated:YES];
65     
66     secondVc.num=(int)indexPath.row;
67     secondVc.arrCity=self.array;
68     
69     
70     NSLog(@"%@",self.array[indexPath.row][@"State"]);
71 }
72 -(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
73 {
74     NSLog(@"%ld",indexPath.row);
75 }

6.SecondTableViewController.h文件中定义几个属性

1 #import <UIKit/UIKit.h>
2 
3 @interface SecondTableViewController : UITableViewController
4 @property(strong,nonatomic)NSArray *arrCity;//用于接受上个页面传来的值
5 @property(strong,nonatomic)NSMutableArray *array;
6 @property(assign,nonatomic)int num;
7 @end

7.SecondTableViewController.m文件中实现如下代码

 1 #import "SecondTableViewController.h"
 2 
 3 @interface SecondTableViewController ()
 4 
 5 @end
 6 
 7 @implementation SecondTableViewController
 8 
 9 - (void)viewDidLoad {
10     [super viewDidLoad];
11     self.title=@"市级";
12     self.array=[NSMutableArray array];
13     for (NSDictionary *dic in self.arrCity[self.num][@"Cities"]) {
14         [self.array addObject:dic[@"city"]];
15     }
16     
17     [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"myCell"];
18     
19 }
20 
21 - (void)didReceiveMemoryWarning {
22     [super didReceiveMemoryWarning];
23     // Dispose of any resources that can be recreated.
24 }
25 
26 #pragma mark - Table view data source
27 
28 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
29 //#warning Incomplete implementation, return the number of sections
30     return 1;
31 }
32 
33 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
34 //#warning Incomplete implementation, return the number of rows
35     return self.array.count;
36 }
37 
38 
39 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
40     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell" forIndexPath:indexPath];
41     
42     cell.textLabel.text=self.array[indexPath.row];
43     
44     return cell;
45 }

 

posted @ 2016-03-17 20:54  远方的狼  阅读(192)  评论(0编辑  收藏  举报