IOSUI课堂笔记用TableView实现省市区
2014-06-19 20:57
344 查看
省市区的实现 MainViewController.h文件下的内容 #import <UIKit/UIKit.h> @interface MainViewController : UIViewController<UITableViewDataSource, UITableViewDelegate> @property (nonatomic, retain)NSMutableArray *provinces; @property (nonatomic, retain)NSMutableArray *cities; @property (nonatomic, retain)NSMutableDictionary *pDic; @property (nonatomic, retain)NSMutableDictionary *cDic; @end MainViewController.m文件下的内容 #import "MainViewController.h" #import "SecondViewController.h" @interface MainViewController () @end @implementation MainViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization NSString *filePath = [[NSBundle mainBundle] pathForResource:@"area" ofType:@"txt"]; NSError *error = nil; NSString *buffer = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error]; if (error) { NSLog(@"error : %@", [error localizedDescription]); exit(1); } NSLog(@"%@", buffer); //把每一行切成数组元素 NSArray *dataArray = [buffer componentsSeparatedByString:@"\n"]; NSLog(@"%@", dataArray); NSMutableArray *province = [NSMutableArray array]; for (NSString *s in dataArray) { //找到省 if (!([s hasPrefix:@" "])) { NSMutableArray *cities = [NSMutableArray array]; NSDictionary *pDic = [NSDictionary dictionaryWithObjectsAndKeys:s, @"name", cities, @"cities", nil]; [province addObject:pDic]; } //找到市 if ([s hasPrefix:@" "] && ![s hasPrefix:@" "]) { NSMutableArray *areas = [NSMutableArray array]; NSDictionary *cDic = [NSDictionary dictionaryWithObjectsAndKeys:s, @"name", areas, @"areas", nil]; NSDictionary *pDic = [province lastObject]; NSMutableArray *cities = [pDic objectForKey:@"cities"]; [cities addObject:cDic]; } //找到区 if ([s hasPrefix:@" "]) { NSDictionary *pDic = [province lastObject]; NSMutableArray *cities = [pDic objectForKey:@"cities"]; NSDictionary *cDic = [cities lastObject]; NSMutableArray *areas = [cDic objectForKey:@"areas"]; [areas addObject:s]; } } // 得到省数组 self.provinces = province; self.title = @"省市区"; } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain]; tableView.dataSource = self; tableView.delegate = self; [self.view addSubview:tableView]; [tableView release]; } // 根据省的数量确定 section 数 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [self.provinces count]; } //给section赋值 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { NSMutableDictionary *pDic = [self.provinces objectAtIndex:section]; NSArray *array = [pDic allValues]; return [array lastObject]; } // 根据所点 省 内 市的数量 确定section内cell的数量 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { self.pDic = [self.provinces objectAtIndex:section]; NSArray *cities = [self.pDic valueForKey: @"cities"]; return [cities count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *str = @"reuse"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str]; if (nil == cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str]; } NSMutableDictionary *pDic = [self.provinces objectAtIndex:indexPath.section]; // 找市 给 cell 赋值 self.cities = [pDic objectForKey:@"cities"]; NSString *value = [[self.cities objectAtIndex:indexPath.row] objectForKey:@"name"]; cell.textLabel.text = value; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { SecondViewController *secondVC = [[SecondViewController alloc] init]; // 将 城市字典 传值 NSDictionary *pDic = [self.provinces objectAtIndex:indexPath.section]; NSMutableArray *cities = [pDic valueForKey:@"cities"]; NSMutableDictionary *cDic = [cities objectAtIndex:indexPath.row]; secondVC.cDDic = cDic; [self.navigationController pushViewController:secondVC animated:YES]; [secondVC release]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end SecondViewController.h下的文件 #import <UIKit/UIKit.h> @interface SecondViewController : UIViewController<UITableViewDataSource, UITableViewDelegate> @property (nonatomic, retain)NSMutableDictionary *cDDic; @end SecondViewContreoller.m下的文件 #import "SecondViewController.h" @interface SecondViewController () @end @implementation SecondViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain]; tableView.dataSource = self; tableView.delegate = self; NSArray *array = [self.cDDic allValues]; self.navigationItem.title = array[0]; [self.view addSubview:tableView]; [tableView release]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSMutableArray *areas = [self.cDDic objectForKey:@"areas"]; return [areas count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *str = @"reuse"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str]; if (nil == cell) { cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str]; } NSMutableArray *areas = [self.cDDic objectForKey:@"areas"]; NSString *value = [areas objectAtIndex:indexPath.row]; cell.textLabel.text = value; return cell; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end
相关文章推荐
- IOS开发笔记——实现tableView的headerView跟随cell滑动
- 通过实现一个 TableView 来理解 iOS UI 编程 (个人感觉写的很棒!)
- iOS开发-UI控件:使用TableView实现多级树型menu
- 通过实现一个 TableView 来理解 iOS UI 编程
- 通过实现一个 TableView 来理解 iOS UI 编程
- ((ios开发学习笔记 十一))自定义TableViewCell 的方式实现自定义TableView(带源码)
- iOS学习笔记-- tableView单选的实现
- ((ios开发学习笔记九)) Simple TableView 实现(附 实例源码)
- 通过实现一个TableView来理解iOS UI编程
- 第二章、小控件实例 tableview,button(iOS学习笔记,从零开始。)
- IOS 学习笔记(9)tableView基础
- IOS学习笔记(8)UIPageViewController UIProgressView 监听和响应键盘通知
- iOS开发笔记--TableView的详细使用
- iOS 使用tableview,实现两个页面跳转及使用协议时错误记录
- IOS开发学习笔记(四)——使用TableView
- ios如何实现被键盘遮挡时,带有textfield的tableview自动上移
- IOS详解TableView——对话聊天布局的实现
- iOS学习笔记---iOS用view实现推出菜单栏的动画效果
- IOS学习笔记之EGOTableViewPullRefresh实现下拉刷新
- iOS学习笔记---数据库SQLite3的基本操作并讲内容显示在tableView中