美文网首页IOS
解析省市区(用UITableView)

解析省市区(用UITableView)

作者: Miss_差不多 | 来源:发表于2017-08-15 20:26 被阅读16次

AppDelegate.m

#import "AppDelegate.h"
#import "ProvinceViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    ProvinceViewController *rootViewController = [[ProvinceViewController alloc] init];
    UINavigationController *rootNavigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
    self.window.rootViewController = rootNavigationController;
   
   return YES;
}

ProvinceViewController.m

#import "ProvinceViewController.h"
#import "CityViewController.h"
@interface ProvinceViewController ()
<
UITableViewDelegate,
UITableViewDataSource
>
@property(nonatomic,retain)NSMutableArray *provinceArray;
@end

@implementation ProvinceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
     self.navigationController.navigationBarHidden = YES;
    //创建省份数组
    self.provinceArray = [NSMutableArray array];
    //加载文件得到字符串,参数1:文件路径 参数2:编码格式 参数3:错误信息
    //调试
    // NSError *error = nil;
    NSString *str = [NSString stringWithContentsOfFile:@"/Users/lanou3g/Desktop/oc/UI06-TableView/UI06-TableView/area.txt" encoding:NSUTF8StringEncoding error:nil];
    //以换行符分割
    NSArray *lineArray = [str componentsSeparatedByString:@"\n"];
    for (NSString *lineString in lineArray) {
        
        if (![lineString hasPrefix:@" "]) {
            //前缀不是空格
            //创建一个省份字典
            NSMutableDictionary *provinceDic = [NSMutableDictionary dictionary];
            //省份字典添加键值对(provinceName:省份名)
            [provinceDic setObject:lineString forKey: @"provinceName"];
            //创建空城市数组(ps:用于后面存储城市字典)
            NSMutableArray *cityArray = [NSMutableArray array];
            //添加键值对(cityArray:城市数组)
            [provinceDic setObject:cityArray forKey: @"cityArray"];
            //将省字典添加到省数组中
            [self.provinceArray addObject:provinceDic];
            
        }else if(![lineString hasPrefix:@"    "]){
            //前缀有空格且不是4个空格的话为城市名
            //取到城市名的时候需要找到该城市所属的省份(当前省份数组最后一个元素就是当前城市所属省份)
            NSMutableDictionary *provinceDic = [self.provinceArray lastObject];
            NSMutableArray *cityArray = [provinceDic objectForKey:@"cityArray"];
            //添加键值对(cityName:城市名)
            NSMutableDictionary *cityDic = [NSMutableDictionary dictionary];
            [cityDic setObject:lineString forKey:@"cityName"];
            //创建地区数组
            NSMutableArray *areaArray = [NSMutableArray array];
            //添加键值对(areaArray:地区数组)
            [cityDic setObject:areaArray forKey:@"areaArray"];
            //将城市字典存入到对应的城市数组中
            [cityArray addObject:cityDic];
            
            
            
        }else{
            //前缀有空格且是4个空格的话为区名
            NSMutableDictionary *provinceDic = [self.provinceArray lastObject];
            NSMutableArray *cityArray = [provinceDic objectForKey:@"cityArray"];
            NSMutableDictionary *cityDic = [cityArray lastObject];
            NSMutableArray *areaArray = [cityDic objectForKey:@"areaArray"];
            [areaArray addObject:lineString];
            
            
        }
    }
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style: UITableViewStylePlain];
    tableView.backgroundColor = [UIColor grayColor];
    //设置行高
    tableView.rowHeight = 100.f;
    tableView.separatorColor = [UIColor greenColor];
    //
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 100)];
    view.backgroundColor = [UIColor greenColor];
    tableView.tableHeaderView = view;
    tableView.delegate = self;
    tableView.dataSource = self;
    
    [self.view addSubview:tableView];
}
//设置某一个section中的行数;
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.provinceArray.count;
    
}
//设置某一位置的cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *province = self.provinceArray[indexPath.row];
    NSString *name = [province objectForKey:@"provinceName"];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    //创建新
    if (nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleValue1 reuseIdentifier: @"cell"];
    }
    cell.textLabel.text = name;
    
    
    
    
    return cell;
}
//当我点击某一个cell的时候会触发该方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *province = self.provinceArray[indexPath.row];
    CityViewController *cityController = [[CityViewController alloc]init];
    cityController.cityArray = [province objectForKey:@"cityArray"];
    [self.navigationController pushViewController:cityController animated:YES];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end

CityViewController.h

@property(nonatomic,retain)NSMutableArray *cityArray;

CityViewController.m

#import "CityViewController.h"
#import "AreaViewController.h"
@interface CityViewController ()
<
UITableViewDelegate,
UITableViewDataSource
>

@end

@implementation CityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style: UITableViewStylePlain];
    tableView.backgroundColor = [UIColor grayColor];
    //设置行高
    tableView.rowHeight = 100.f;
    tableView.separatorColor = [UIColor redColor];
    //
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 100)];
    view.backgroundColor = [UIColor greenColor];
    tableView.tableHeaderView = view;
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
}

//设置某一个section中的行数;
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.cityArray.count;
    
}
//设置某一位置的cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *province = self.cityArray[indexPath.row];
    NSString *name = [province objectForKey:@"cityName"];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    //创建新
    if (nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleValue1 reuseIdentifier: @"cell"];
    }
    cell.textLabel.text = name;

    return cell;
}
//当我点击某一个cell的时候会触发该方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *province = self.cityArray[indexPath.row];
    AreaViewController *areaController = [[AreaViewController alloc]init];
    areaController.areaArray = [province objectForKey:@"areaArray"];
    [self.navigationController pushViewController:areaController animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end

AreaViewController.h

@property(nonatomic,retain)NSMutableArray *areaArray;

AreaViewController.m

#import "AreaViewController.h"

@interface AreaViewController ()
<
UITableViewDelegate,
UITableViewDataSource
>
@end

@implementation AreaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style: UITableViewStylePlain];
    tableView.backgroundColor = [UIColor grayColor];
    //设置行高
    tableView.rowHeight = 100.f;
    tableView.separatorColor = [UIColor redColor];
    //
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 100)];
    view.backgroundColor = [UIColor greenColor];
    tableView.tableHeaderView = view;
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
}

//设置某一个section中的行数;
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.areaArray.count;
    
}
//设置某一位置的cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
     NSString *name = self.areaArray[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    //创建新
    if (nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleValue1 reuseIdentifier: @"cell"];
    }
    cell.textLabel.text = name;
   return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    }

@end

相关文章

网友评论

    本文标题:解析省市区(用UITableView)

    本文链接:https://www.haomeiwen.com/subject/khgvrxtx.html