解析省市區(qū)(用UITableView)

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;
    //創(chuàng)建省份數(shù)組
    self.provinceArray = [NSMutableArray array];
    //加載文件得到字符串,參數(shù)1:文件路徑 參數(shù)2:編碼格式 參數(shù)3:錯誤信息
    //調(diào)試
    // 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:@" "]) {
            //前綴不是空格
            //創(chuàng)建一個省份字典
            NSMutableDictionary *provinceDic = [NSMutableDictionary dictionary];
            //省份字典添加鍵值對(provinceName:省份名)
            [provinceDic setObject:lineString forKey: @"provinceName"];
            //創(chuàng)建空城市數(shù)組(ps:用于后面存儲城市字典)
            NSMutableArray *cityArray = [NSMutableArray array];
            //添加鍵值對(cityArray:城市數(shù)組)
            [provinceDic setObject:cityArray forKey: @"cityArray"];
            //將省字典添加到省數(shù)組中
            [self.provinceArray addObject:provinceDic];
            
        }else if(![lineString hasPrefix:@"    "]){
            //前綴有空格且不是4個空格的話為城市名
            //取到城市名的時候需要找到該城市所屬的省份(當(dāng)前省份數(shù)組最后一個元素就是當(dāng)前城市所屬省份)
            NSMutableDictionary *provinceDic = [self.provinceArray lastObject];
            NSMutableArray *cityArray = [provinceDic objectForKey:@"cityArray"];
            //添加鍵值對(cityName:城市名)
            NSMutableDictionary *cityDic = [NSMutableDictionary dictionary];
            [cityDic setObject:lineString forKey:@"cityName"];
            //創(chuàng)建地區(qū)數(shù)組
            NSMutableArray *areaArray = [NSMutableArray array];
            //添加鍵值對(areaArray:地區(qū)數(shù)組)
            [cityDic setObject:areaArray forKey:@"areaArray"];
            //將城市字典存入到對應(yīng)的城市數(shù)組中
            [cityArray addObject:cityDic];
            
            
            
        }else{
            //前綴有空格且是4個空格的話為區(qū)名
            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];
    //設(shè)置行高
    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];
}
//設(shè)置某一個section中的行數(shù);
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.provinceArray.count;
    
}
//設(shè)置某一位置的cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *province = self.provinceArray[indexPath.row];
    NSString *name = [province objectForKey:@"provinceName"];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    //創(chuàng)建新
    if (nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleValue1 reuseIdentifier: @"cell"];
    }
    cell.textLabel.text = name;
    
    
    
    
    return cell;
}
//當(dāng)我點擊某一個cell的時候會觸發(fā)該方法
-(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];
    //設(shè)置行高
    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];
}

//設(shè)置某一個section中的行數(shù);
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.cityArray.count;
    
}
//設(shè)置某一位置的cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *province = self.cityArray[indexPath.row];
    NSString *name = [province objectForKey:@"cityName"];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    //創(chuàng)建新
    if (nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleValue1 reuseIdentifier: @"cell"];
    }
    cell.textLabel.text = name;

    return cell;
}
//當(dāng)我點擊某一個cell的時候會觸發(fā)該方法
-(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];
    //設(shè)置行高
    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];
}

//設(shè)置某一個section中的行數(shù);
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.areaArray.count;
    
}
//設(shè)置某一位置的cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
     NSString *name = self.areaArray[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    //創(chuàng)建新
    if (nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleValue1 reuseIdentifier: @"cell"];
    }
    cell.textLabel.text = name;
   return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    }

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容