//
// MainViewController.m
// UI10_带分区的省市区
//
// Created by dllo on 15/8/11.
// Copyright (c) 2015年 zhozhicheng. All rights reserved.
// #import "MainViewController.h"
#import "SecondViewController.h"
@interface MainViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)NSMutableArray *proArr;
@property(nonatomic,retain)UITableView *tableView;
@end @implementation MainViewController
-(void)dealloc
{
[_proArr release];
[super dealloc];
}
#pragma mark 假设在初始化方法里使用self.view,此时还没有创建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/上课内容 /UI10_带分区的省市区/UI10_带分区的省市区/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 setValue:temp forKey:@"cityName"];
NSMutableArray *zoneArr=[NSMutableArray array];
[cityDic setValue:zoneArr forKey:@"zoneArr"];
NSMutableDictionary *proDic=[self.proArr lastObject];
NSMutableArray *cityArr=proDic[@"cityArr"];
[cityArr addObject:cityDic];
}else
{
NSMutableDictionary *proDic=[self.proArr lastObject];
NSMutableArray *cityArr=proDic[@"cityArr"];
NSMutableDictionary *cityDic=[cityArr lastObject];
NSMutableArray *zoneArr=cityDic[@"zoneArr"];
[zoneArr addObject:temp];
} } } - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor=[UIColor cyanColor];
self.navigationController.navigationBar.translucent=NO;
self.navigationItem.title=@"省";
self.tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height -64) style:UITableViewStylePlain];
self.tableView.dataSource=self;
self.tableView.delegate=self;
[self.view addSubview:self.tableView];
[self.tableView release]; }
//
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{ NSMutableArray *cityArr =self.proArr[section][@"cityArr"];
return cityArr.count;
} -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.proArr.count;
}
//很多其它 -(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 *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 70, 20)];
[newView addSubview:label];
[label release];
label.text=self.proArr[section][@"proName"]; UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(300, 0, 40, 20);
[button setTitle:@"很多其它" forState:UIControlStateNormal];
[newView addSubview:button];
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside]; return newView;
} -(void)click:(UIButton *)button
{
NSLog(@"da");
} //创建cell,显示数据
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ static NSString *reuse=@"reuse";
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:reuse];
if (!cell) {
cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
}
//省字典
NSMutableDictionary *proDic=self.proArr[indexPath.section];
NSMutableArray *cityArr=proDic[@"cityArr"];
cell.textLabel.text=cityArr[indexPath.row][@"cityName"]; return cell;
}
//调到第二个页面 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
SecondViewController *secondVC=[[SecondViewController alloc] init];
[self.navigationController pushViewController:secondVC animated:YES];
[secondVC release];
} //分区头标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return self.proArr[section][@"proName"];
} - (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
// UI10_带分区的省市区
//
// Created by dllo on 15/8/11.
// Copyright (c) 2015年 zhozhicheng. All rights reserved.
// #import "SecondViewController.h" @interface SecondViewController () @end @implementation SecondViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor=[UIColor cyanColor];
} - (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

最新文章

  1. EF7 使用 K EF 异常
  2. BFC and Haslayout
  3. hrbustoj 1429:凸多边形(计算几何,判断点是否在多边形内,二分法)
  4. linux C(hello world)三个数最大和三个数最新
  5. 计划:怎样理解水平集方法 ITK Level set V4 框架介绍
  6. asp.net中用回车代替按钮事件
  7. js大小写锁判断
  8. Linux下php+mysql+nginx编译搭建(一)
  9. spring揭秘 读书笔记 一 IoC初探
  10. ie6下固定位置的实现
  11. 将markdown文档使用gulp转换为HTML【附带两套css样式】
  12. 删除csdn上面自己上传的资源
  13. D. Diverse Garland
  14. Guava 12:Guava EventBus源码剖析
  15. C++ 50学习 之提高对 C++的认识
  16. 【慕课网实战】Spark Streaming实时流处理项目实战笔记十四之铭文升级版
  17. 两个有序数组长度分别为m,n,最多m+n次查找找出相同的数
  18. php对于url提交数据的获取办法
  19. 手动生成moc文件
  20. 关于Linux系统指令 top 之 %si 占用高,分析实例一

热门文章

  1. Linux多线程实践(一)线程基本概念和理论
  2. 2015 Multi-University Training Contest 2 1006 Friends 壮压
  3. Android学习笔记之ProgressBar案例分析
  4. Android监听程序自身被卸载
  5. UVA 10539 - Almost Prime Numbers 素数打表
  6. UVA 11426 - GCD - Extreme (II) 欧拉函数-数学
  7. MAME 0.201 发布,重温童年的街机模拟器
  8. c12---数组
  9. JPA实现一对多(OneToMany)关联
  10. CentOS7开启网络配置