iOS UI10_带分区的省市区

//
//  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

posted @ 2017-06-22 14:31  jhcelue  阅读(128)  评论(0编辑  收藏  举报