带分区的省市区字典数组

来源:互联网 发布:股票网络销售话术 编辑:程序博客网 时间:2024/05/20 16:01

准备工作不再赘述

一个tableView TitleForHeader为省名,分区个数为省的个数,每个分区中的cell.textLabel.text为省对应的市名

MainViewController.m

#import "MainViewController.h"#import "SecondViewController.h"@interface MainViewController ()<UITableViewDataSource,UITableViewDelegate>@property(nonatomic,retain)NSMutableArray *proArr;@property(nonatomic,retain)NSMutableArray *cityArr;@property(nonatomic,retain)NSMutableArray *zoneArr;@end@implementation MainViewController-(void)dealloc{    [_proArr release];    [_cityArr release];    [_zoneArr release];    [super dealloc];}#warning 如果在初始化方法里使用self.view,此时还没有创建self.view,系统会自动调用loadView,创建一个self.view,从而改变VC的运行流程,所以我们只在初始化方法里初始化容器等数据部分,不创建视图-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        [self createData];    }    return self;}-(void)createData{    NSString *path=@"/Users/dllo/Desktop/UINote/UI 10-带分区的省市区/UI 10-带分区的省市区/area(1).txt";    NSString *str=[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];    NSArray *strArr=[str componentsSeparatedByString:@"\n"];    self.proArr=[NSMutableArray array];    for (NSString *temp in strArr) {        if (![temp hasPrefix:@" "]) {            // 创建一个省字典            NSMutableDictionary *proDic=[NSMutableDictionary dictionary];            // 将省名添加到省字典中            [proDic setObject:temp forKey:@"proName"];            // 创建一个市数组            self.cityArr=[NSMutableArray array];            // 将市数组添加到省字典中            [proDic setObject:self.cityArr forKey:@"cityArr"];            // 将省字典放到省数组中            [self.proArr addObject:proDic];        }        else if ([temp hasPrefix:@"  "]&&![temp hasPrefix:@"    "])        {            // 找到对应的城市            // 创建一个市字典            NSMutableDictionary *cityDic=[NSMutableDictionary dictionary];            // 将市名放到市字典中            [cityDic setObject:temp forKey:@"cityName"];            // 创建一个区数组            self.zoneArr=[NSMutableArray array];            // 将区数组添加到市字典中            [cityDic setObject:self.zoneArr forKey:@"zoneArr"];            // 给市字典找一个位置            // 市字典在省字典中            // 省字典在省数组中            NSMutableDictionary *proDic=[self.proArr lastObject];            self.cityArr=proDic[@"cityArr"];            [self.cityArr addObject:cityDic];        }        else{            // 先找到省字典            NSMutableDictionary *proDic=[self.proArr lastObject];            // 找到市数组            self.cityArr=proDic[@"cityArr"];            // 找到市字典            NSMutableDictionary *cityDic=[self.cityArr lastObject];            // 找到区数组            self.zoneArr=cityDic[@"zoneArr"];            [self.zoneArr addObject:temp];        }    }}- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.    self.view.backgroundColor=[UIColor yellowColor];    self.navigationController.navigationBar.translucent=NO;    // 铺一个tableView    UITableView *tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-64) style:UITableViewStylePlain];    [self.view addSubview:tableView];    tableView.dataSource=self;    tableView.delegate=self;    [tableView release];}-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    return self.proArr.count;}-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{    return self.proArr[section][@"proName"];}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    for (int i = 0; i < self.proArr.count; i ++) {    if (section == i) {        NSArray *cityArr = self.proArr[section][@"cityArr"];        return cityArr.count;    }}    return 0;}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *reuse=@"reuse";    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:reuse];    if (!cell) {        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];    }//    NSMutableArray *cityArr=self.proArr[indexPath.section][@"cityArr"];//    cell.textLabel.text=cityArr[indexPath.row][@"cityName"];    cell.textLabel.text=self.proArr[indexPath.section][@"cityArr"][indexPath.row][@"cityName"];    return cell;}-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{    UIView *newView=[[[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 30)] autorelease];    newView.backgroundColor=[UIColor redColor];    UILabel *textLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 50, 20)];    [newView addSubview:textLabel];    [textLabel release];    textLabel.text=self.proArr[section][@"proName"];    UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];    [button setTitle:@"更多" forState:UIControlStateNormal];    [newView addSubview:button];    return newView;}-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    // 找到省字典//    NSMutableDictionary *proDic = self.proArr[indexPath.row];//    //省字典对应的市数组//    self.cityArr = proDic[@"cityArr"];    SecondViewController *secVC=[[SecondViewController alloc]init];    [self.navigationController pushViewController:secVC animated:YES];//    secVC.arr = self.cityArr;    [secVC release];}
0 0