您的位置:首页 > 产品设计 > UI/UE

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: