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

UITableView 数组与字典结合(省市区)

2014-06-21 20:09 295 查看

1.MainViewController

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;

NSLog(@"%@", self.provinces);

self.title = @"省市区";

// 设置TableView

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];

}

// 2.SecondViewcontroller

- (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;

}

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