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)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor cyanColor];

    //这种设置,需要在高度基础上-64
//    self.navigationController.navigationBar.translucent=NO;

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

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

    [self createData];

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

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




}

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.proArr=[NSMutableArray array];
    }
    return self;
}

-(void)createData{
    NSString *path=@"/Users/dllo/Desktop/UI/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];
        }
    } 

}



-(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 *proReuse=@"proReuse";
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:proReuse];
        if (!cell) {
            cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:proReuse] autorelease];
        }
        cell.textLabel.text=self.proArr[indexPath.row][@"name"];

        return cell;

    }else if (self.cityTableView==tableView){
        static NSString *cityReuse=@"cityReuse";
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cityReuse];
        if (!cell) {
            cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cityReuse] autorelease];
        }

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

        return cell;

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

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

        return cell;
    }

}

//点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //需要判断点击的是哪一个tableView
    //方法一. 控件写成属性,然后判断地址是否相同
    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];
    }

    //方法二.通过tag值,和tableView对象进行比较
//    if (self.proTableView.tag==1000) {
//
//    }




}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值