三个tableView联动

#import "RootViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>

@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;
@end

@implementation RootViewController

- (void)dealloc
{
    [_proTableView release];
    [_cityTableView release];
    [_zoneTableView release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor yellowColor];
    //这个设置,需要在高度的基础上-64
    self.navigationController.navigationBar.translucent = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    //这个属性只会让第一个滚动视图适应屏幕,从第二个开始就不管了,要是像保留半透明效果就可以把属性设置成NO,
    self.proArr = [NSMutableArray array];

    self.proTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH/3, HEIGHT) style:UITableViewStylePlain];
    [self.view addSubview:_proTableView];
    [_proTableView release];
    self.proTableView.tag = 1000;
    self.proTableView.dataSource = self;
    self.proTableView.delegate = self;


    self.cityTableView = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH/3, 0, WIDTH/3, HEIGHT-64) style:UITableViewStylePlain];
    [self.view addSubview:_cityTableView];
    [_cityTableView release];
    self.cityTableView.tag = 1001;
    self.cityTableView.dataSource = self;
    self.cityTableView.delegate = self;

    self.zoneTableView = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH*2/3, 0, WIDTH/3, HEIGHT-64) style:UITableViewStylePlain];
    [self.view addSubview:_zoneTableView];
    [_zoneTableView release];
    self.zoneTableView.tag = 1002;
    self.zoneTableView.dataSource = self;
    self.zoneTableView.delegate = self;


    [self createData];

}

-(void)createData{

NSString *path = @"/Users/dllo/Desktop/UI11_三个tableView联动/UI11_三个tableView联动/area.txt";
    NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSArray *strArr = [str componentsSeparatedByString:@"\n"];

    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 (NSDictionary *dic in self.proArr) {
//        NSLog(@"%@",dic[@"name"]);
//        NSArray *cityArr = dic[@"cityArr"];
//        for (NSDictionary *dic in cityArr) {
//            NSLog(@"%@",dic[@"cityName"]);
//            NSArray *zoneArr = dic[@"zoneArr"];
//            for (NSString *str in zoneArr) {
//                NSLog(@"%@",str);
//            }
//        }
//    }


}



-(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;
    }

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.proTableView == tableView){
        static NSString *reuse = @"reuse";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse] autorelease];
        }
        NSDictionary *dic = self.proArr[indexPath.row];

        cell.textLabel.text = dic[@"name"];

        return cell;
    }else if (self.cityTableView == tableView){
        //创建市tableview的cell
        static NSString *reuse = @"reuse";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse] autorelease];
        }
        NSDictionary *dic = self.cityArr[indexPath.row];

        cell.textLabel.text = dic[@"cityName"];

        return cell;
    }else{
        static NSString *reuse = @"reuse";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse] autorelease];
        }

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

        return cell;
    }


}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//需要判断点击的是哪一个tableview
//    if (self.proTableView == tableView)
//1种是通过tag值,和协议方法的tableview对象进行比较
    //2种是空间写成属性,然后判断地址是否相同
    if (self.proTableView == tableView) {
        self.cityArr = self.proArr[indexPath.row][@"cityArr"];
        [self.cityTableView reloadData];
    }

    else if (self.cityTableView == tableView) {
        self.zoneArr = self.cityArr [indexPath.row][@"zoneArr"];
        [self.zoneTableView reloadData];
    }

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值