UI0_带分区的省市区
2015-08-13 21:20
603 查看
// // AppDelegate.m // UI0_带分区的省市区 // // Created by dllo on 15/8/11. // Copyright (c) 2015年 Clare. All rights reserved. // #import "AppDelegate.h" #import "MainViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; [_window release]; MainViewController *mainVC = [[MainViewController alloc] init]; UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:mainVC]; self.window.rootViewController = naVC; [mainVC release]; [naVC release]; return YES; }
// // MainViewController.m // UI0_带分区的省市区 // // Created by dllo on 15/8/11. // Copyright (c) 2015年 Clare. All rights reserved. // #import "MainViewController.h" #import "SecondViewController.h" @interface MainViewController ()<UITableViewDataSource,UITableViewDelegate> @property(nonatomic, retain)NSMutableArray *proArr; @end @implementation MainViewController // 只初始化容器,只要没有self.view系统会自动调用loaddidview创建self.view改变运行流程 #warning 如果在初始化方法里使用selfview,此时还没有创建self.view,系统会自动调用loadview,创建一个self.view,从而改变VC的运行流程,所以我们只在初始化方法里初始化容器等数据部分,不创建视图 - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { [self createData]; } return self; } - (void)createData { NSString *path = @"/Users/dllo/Desktop/Clare/OC/OC_省市区字典数组/OC_省市区字典数组/area.txt"; NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; NSArray *strArr = [str componentsSeparatedByString:@"\n"]; self.proArr = [NSMutableArray array]; for (NSString *temp in strArr) { if (![temp hasPrefix:@" "]) { NSMutableDictionary *proDic = [NSMutableDictionary dictionary]; [proDic setObject:temp forKey:@"proName"]; NSMutableArray *cityArr = [NSMutableArray array]; [proDic setObject:cityArr forKey:@"cityArr"]; [self.proArr addObject:proDic]; } else if ([temp hasPrefix:@" "] && ![temp hasPrefix:@" "]){ NSMutableDictionary *cityDic = [NSMutableDictionary dictionary]; [cityDic setObject:temp forKey:@"cityName"]; NSMutableArray *zoneArr = [NSMutableArray array]; [cityDic setObject:zoneArr forKey:@"zoneArr"]; NSMutableDictionary *proDic = [self.proArr lastObject]; NSMutableArray *cityArr = proDic[@"cityArr"]; [cityArr addObject:cityDic]; } else if ([temp hasPrefix:@" "]) { NSMutableDictionary *proDic = [self.proArr lastObject]; NSMutableArray *cityArr = proDic[@"cityArr"]; NSMutableDictionary *cityDic = [cityArr lastObject]; NSMutableArray *zoneArr = cityDic[@"zoneArr"]; [zoneArr addObject:temp]; } } } - (void)dealloc { [_proArr release]; [super dealloc]; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.navigationController.navigationBar.translucent = NO; UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain]; [self.view addSubview:tableView]; [tableView release]; self.title = @"省"; tableView.delegate = self; tableView.dataSource = self; } // 一共有多少个分区 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // 数组中有多少个对象返回多少个分区 return self.proArr.count; } // 一个分区有多少行 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // 省对应的市数组 NSMutableArray *cityArr = self.proArr[section][@"cityArr"]; return cityArr.count; } // 分区的名字省名 和更多添加按钮中的label必须同时存在 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return self.proArr[section][@"proName"]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *reuse = @"reuse"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse]; if (!cell) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse] autorelease]; } // 根据分区先找到省字典 NSMutableDictionary *proDic = self.proArr[indexPath.section]; NSMutableArray *cityArr = proDic[@"cityArr"]; cell.textLabel.text = cityArr[indexPath.row][@"cityName"]; return cell; } // 添加更多按钮 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *newView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 20)] autorelease]; newView.backgroundColor = [UIColor yellowColor]; UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, 100, 20)]; [newView addSubview:textLabel]; [textLabel release]; // 根据sextion确定label的名字 textLabel.text = self.proArr[section][@"proName"]; UIButton *moreButton = [UIButton buttonWithType:UIButtonTypeSystem]; moreButton.frame = CGRectMake(320, 5, 50, 20); [moreButton setTitle:@"更多..." forState:UIControlStateNormal]; [newView addSubview:moreButton]; moreButton.backgroundColor = [UIColor whiteColor]; moreButton.layer.borderWidth = 1; moreButton.layer.cornerRadius = 10; moreButton.layer.masksToBounds = YES; [moreButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside]; return newView; } - (void)click:(UIButton *)but { } - (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.m // UI0_带分区的省市区 // // Created by dllo on 15/8/11. // Copyright (c) 2015年 Clare. All rights reserved. // #import "SecondViewController.h" @interface SecondViewController () @end @implementation SecondViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. } - (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
相关文章推荐
- Ajax请求中的async:false/true的作用
- UI0810通讯录加强版
- 黑马程序员——Java基础--GUI(图形化界面)
- UI09_自定义cell
- zoj 2966 Build The Electric System
- leetcode 115: Distinct Subsequences
- 1007. Maximum Subsequence Sum (25) -- 动态规划
- UI09_多种TableView
- UI11_block练习
- UINavigationController && UIStatusBar 导航控制器 && 状态栏
- 让UILabel 或者 UIButton 的大小和它的内容一样大
- POJ 1458 Common Subsequence LCS
- poj 2299 Ultra-QuickSort【归并排序基础应用】
- iOS基础-UIKit框架-多控制器管理-UINavigationController、控制器的生命周期
- LeetCode-Implement Stack using Queues
- HDOJ 1711 Number Sequence(KMP模板题)
- java GUI(鼠标键盘事件)
- UILabel用法大全
- UITabBarController 标签导航控制器的使用
- UITabBarItem 快捷菜单