Objective-C 省市区 三个tableView联动


// 所有方法都在根视图下
// 定义属性

@property(nonatomic,retain)UITableView *proTableView;

@property(nonatomic,retain)UITableView *cityTableView;

@property(nonatomic,retain)UITableView *zoneTableView;

@property(nonatomic,retain)NSMutableArray *proArr;

@property(nonatomic,retain)NSMutableArray *cityArr;

@property(nonatomic,retain)NSMutableArray *zoneArr;


self.view.backgroundColor = [UIColor whiteColor];

    // 第一种方式这个设置,需要在原有基础上-64

    self.navigationController.navigationBar.translucent = NO;

    

    // 第二种方式

    // 这个属性只会让第一个滚动视图适应屏幕,从第二个开始就不管了,要是想保留半透明效果可以把属性设置成NO,然后再设置滚动视图的坐标位置

//    self.automaticallyAdjustsScrollViewInsets = NO;

    

       // 省tableView

    self.proTableView = [[UITableView allocinitWithFrame:CGRectMake(00WIDTH / 3HEIGHT - 64style:UITableViewStylePlain];

    self.proTableView.dataSource = self;

    self.proTableView.delegate = self;

    self.proTableView.tag = 1000;

    [self.view addSubview:self.proTableView];

    [_proTableView release];


    // 市tableView  

self.cityTableView = [[UITableView allocinitWithFrame:CGRectMake(WIDTH / 30WIDTH / 3HEIGHT - 64style:UITableViewStylePlain];

    self.cityTableView.delegate = self;

    self.cityTableView.dataSource = self;

    self.cityTableView.tag = 1001;

    [self.view addSubview:_cityTableView];

    [_cityTableView release];



    // 区tableView

    self.zoneTableView = [[UITableView allocinitWithFrame:CGRectMake(WIDTH / 3 * 20WIDTH / 3HEIGHT - 64style:UITableViewStylePlain];

   // self.zoneTableView.backgroundColor = [UIColor greenColor];

    self.zoneTableView.delegate = self;

    self.zoneTableView.dataSource = self;

    [self.view addSubview:_zoneTableView];

    [_zoneTableView release];


    // 定义一个方法,遍历省市区

    [self createData];


          // 省市区遍历

- (void)createData{


    NSString *path = @"/Users/dllo/Desktop/资料库/07文件/area.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:@"name"];

            NSMutableArray *cityArr = [NSMutableArray array];

            [proDic setObject:cityArr forKey:@"cityArr"];

            [self.proArr addObject:proDic];

        }else if ([temp hasPrefix:@"  "] && ![temp hasPrefix:@"    "]){

        

            NSMutableDictionary *cityDic = [NSMutableDictionary dictionary];

            [cityDic setObject:temp forKey:@"cityName"];

            NSMutableArray *zoneArr = [NSMutableArray array];

            [cityDic setObject:zoneArr forKey:@"zoneArr"];

            

            NSMutableDictionary *proDic = [self.proArr lastObject];

            NSMutableArray *cityArr = proDic[@"cityArr"];

            [cityArr addObject:cityDic];

            

        }else{

        

            NSMutableDictionary *proDic = [self.proArr lastObject];

            NSMutableArray *cityArr = proDic[@"cityArr"];

            

            NSMutableDictionary *cityDic = [cityArr lastObject];

            NSMutableArray *zoneArr = cityDic[@"zoneArr"];

            [zoneArr addObject:temp];

        }

    }

    

    

    for (NSMutableDictionary *proDic in self.proArr) {

        NSLog(@"%@",proDic[@"name"]);

        

        for (NSMutableDictionary *cityDic in proDic[@"cityArr"]) {

            NSLog(@"%@",cityDic[@"cityName"]);

            

            for (NSString *temp in cityDic[@"zoneArr"]) {

                NSLog(@"%@",temp);

            }

        }

        

    }


}


// 判断当前在哪个tableView

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{


    if (self.proTableView == tableView) {

        

        return self.proArr.count;

        

    }else if (self.cityTableView == tableView){

        

        return self.cityArr.count;

        

    }else{

    

        return self.zoneArr.count;

    }

}



// 判断当前在哪个tableView,显示对应的值

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{


    if (self.proTableView == tableView) {

        NSString *reuse = @"reuse";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];

        

        if (!cell) {

            

            cell = [[[UITableViewCell allocinitWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];

        }

        cell.textLabel.text = self.proArr[indexPath.row][@"name"];

        return cell;

        

    }else if (self.cityTableView == tableView){

    

        // 创建市tableviewcell

        NSString *reuse = @"reuse";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];

        if (!cell) {

            cell = [[[UITableViewCell allocinitWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];

        }

        

        cell.textLabel.text = self.cityArr[indexPath.row][@"cityName"];

        return cell;

        

    }

    else{

    

        NSString *reuse = @"reuse";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];

        if (!cell) {

            cell = [[[UITableViewCell allocinitWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];

        }

        cell.textLabel.text = self.zoneArr[indexPath.row];

        return cell;

    

    }


}


// 点击方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

   

    

    // 需要判断点击的是哪一个tableview

    if (tableView.tag == 1000) {

        // 或者用属性地址:self.proTableView == tableView

        self.cityArr = self.proArr[indexPath.row][@"cityArr"];

        [self.cityTableView reloadData];


                   // 用来消除一个小bug,可以把这句话注掉试一下

        self.zoneArr = NULL;

  

        [self.zoneTableView reloadData];

        

    }else if (tableView.tag == 1001){

    

        self.zoneArr = self.cityArr[indexPath.row][@"zoneArr"];

        [self.zoneTableView reloadData];

    }


}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值