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;
}
相关文章推荐
- UI_UITableView省市区字典数组
- UI09_UITableView省市区字典数组
- UITableView省市区字典
- iOS UI08_tableView省市区字典数组
- 一个TableView实现省市区字典数组的跳转
- iOS UI08_tableView省市区字典数组
- TableView省市区数组字典 属性传值
- UI09_多种UITableView(一个界面出现三个tableView,省市区数组)
- UITableView省市区数组
- 带分区的UITableView的创建(省市区字典)
- iOS使用UITableView从plist中选择省市区
- iOS OC06_省市区字典数组
- UIscrollview和UItableView结合使用(就是cell可以滑动改变图片)
- 基于 Objective-C OC的省市区代码(字典数组嵌套)
- UITableView-多组数组展示
- 理解:字典转模型,代理,UItableView的数据源,自定义cell
- 如何结合CoreData给UITableView的section填充数据
- "IOS"空件TableView中,字典,数组的套用........
- OC省市区数组套字典小结
- UITableView 避免奔溃的数组越界处理方法