UI0_带分区的省市区

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值