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

IOSUI课堂笔记用TableView实现省市区

2014-06-19 20:57 344 查看
省市区的实现
MainViewController.h文件下的内容
#import <UIKit/UIKit.h>

@interface MainViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)NSMutableArray *provinces;

@property (nonatomic, retain)NSMutableArray *cities;

@property (nonatomic, retain)NSMutableDictionary *pDic;

@property (nonatomic, retain)NSMutableDictionary *cDic;

@end

MainViewController.m文件下的内容

#import "MainViewController.h"
#import "SecondViewController.h"

@interface MainViewController ()

@end

@implementation MainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

if (self) {
// Custom initialization

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"area" ofType:@"txt"];
NSError *error = nil;
NSString *buffer = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding  error:&error];
if (error) {
NSLog(@"error : %@", [error localizedDescription]);
exit(1);
}
NSLog(@"%@", buffer);

//把每一行切成数组元素
NSArray *dataArray = [buffer componentsSeparatedByString:@"\n"];
NSLog(@"%@", dataArray);

NSMutableArray *province = [NSMutableArray array];
for (NSString *s in dataArray) {
//找到省
if (!([s hasPrefix:@" "])) {
NSMutableArray *cities = [NSMutableArray array];
NSDictionary *pDic = [NSDictionary dictionaryWithObjectsAndKeys:s, @"name", cities, @"cities", nil];
[province addObject:pDic];
}
//找到市
if ([s hasPrefix:@"  "] && ![s hasPrefix:@"    "]) {
NSMutableArray *areas = [NSMutableArray array];
NSDictionary *cDic = [NSDictionary dictionaryWithObjectsAndKeys:s, @"name", areas, @"areas", nil];

NSDictionary *pDic = [province lastObject];
NSMutableArray *cities = [pDic objectForKey:@"cities"];
[cities addObject:cDic];
}
//找到区
if ([s hasPrefix:@"    "]) {
NSDictionary *pDic = [province lastObject];
NSMutableArray *cities = [pDic objectForKey:@"cities"];
NSDictionary *cDic = [cities lastObject];
NSMutableArray *areas = [cDic objectForKey:@"areas"];
[areas addObject:s];
}

}

// 得到省数组
self.provinces = province;

self.title = @"省市区";

}

return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain];

tableView.dataSource = self;
tableView.delegate = self;

[self.view addSubview:tableView];
[tableView release];

}

// 根据省的数量确定 section 数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.provinces count];

}

//给section赋值
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSMutableDictionary *pDic = [self.provinces objectAtIndex:section];
NSArray *array = [pDic allValues];
return [array lastObject];

}

// 根据所点 省 内 市的数量 确定section内cell的数量
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
self.pDic = [self.provinces objectAtIndex:section];
NSArray *cities = [self.pDic valueForKey: @"cities"];

return [cities count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *str = @"reuse";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
if (nil == cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];
}

NSMutableDictionary *pDic = [self.provinces objectAtIndex:indexPath.section];

// 找市 给 cell 赋值
self.cities = [pDic objectForKey:@"cities"];
NSString *value = [[self.cities objectAtIndex:indexPath.row] objectForKey:@"name"];
cell.textLabel.text = value;

return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
SecondViewController *secondVC = [[SecondViewController alloc] init];

// 将 城市字典 传值
NSDictionary *pDic = [self.provinces objectAtIndex:indexPath.section];

NSMutableArray *cities = [pDic valueForKey:@"cities"];
NSMutableDictionary *cDic = [cities objectAtIndex:indexPath.row];
secondVC.cDDic = cDic;

[self.navigationController pushViewController:secondVC animated:YES];
[secondVC release];

}

- (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.h下的文件
#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)NSMutableDictionary *cDDic;

@end

SecondViewContreoller.m下的文件

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization

}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain];

tableView.dataSource = self;
tableView.delegate = self;

NSArray *array = [self.cDDic allValues];

self.navigationItem.title = array[0];

[self.view addSubview:tableView];
[tableView release];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSMutableArray *areas = [self.cDDic objectForKey:@"areas"];
return [areas count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *str = @"reuse";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
if (nil == cell) {
cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];
}

NSMutableArray *areas = [self.cDDic objectForKey:@"areas"];
NSString *value = [areas objectAtIndex:indexPath.row];
cell.textLabel.text = value;

return cell;

}

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