iOS UI09_多种Tableview

//
//  MainViewController.m
//  UI09_多种Tableview
//
//  Created by dllo on 15/8/10.
//  Copyright (c) 2015年 zhozhicheng. All rights reserved.
//

#import "MainViewController.h"

@interface MainViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)NSMutableArray *proArr;
@property(nonatomic,retain)UITableView *tableView1;
@property(nonatomic,retain)UITableView *tableView2;
@property(nonatomic,retain)UITableView *tableView3;
@property(nonatomic,retain)NSMutableArray *cityArr;
@property(nonatomic,retain)NSMutableArray *zoneArr;

@end

@implementation MainViewController
-(void)dealloc
{
    [_proArr release];
    [_cityArr release];
    [_zoneArr release];
    [super dealloc];
}
-(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/作业 /UI08_tableView省市区字典数组/UI08_tableView省市区字典数组/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:@"proName"];
            NSMutableArray *cityArr=[NSMutableArray array];
            [proDic setObject:cityArr forKey:@"cityArr"];
            [self.proArr addObject:proDic];
        }else if ([temp hasPrefix:@"  "] && ![temp hasPrefix:@"    "])
        {
            NSMutableDictionary *cityDic=[NSMutableDictionary dictionary];
            [cityDic setValue:temp forKey:@"cityName"];
            NSMutableArray *zoneArr=[NSMutableArray array];
            [cityDic setValue: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];
        }

    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"c8abca078d303faf9aa32506ef1927a2.jpg"]];
    self.view.alpha=0.5;
    self.navigationController.navigationBar.translucent=NO;

    //一个页面有三个tableView,进行相互联动,点击省显示相应的市,点击市显示相应的区
    //铺设三个tableView

    self.tableView1=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width / 3, self.view.frame.size.height-64) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView1];
    [self.tableView1 release];
    self.tableView1.delegate=self;
    self.tableView1.dataSource=self;
    self.tableView1.separatorStyle = UITableViewCellSeparatorStyleNone;


    self.tableView2=[[UITableView alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 3, 0, self.view.frame.size.width / 3, self.view.frame.size.height-64) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView2];
    [self.tableView2 release];
    self.tableView2.delegate=self;
    self.tableView2.dataSource=self;
       self.tableView2.separatorStyle = UITableViewCellSeparatorStyleNone;


    self.tableView3=[[UITableView alloc] initWithFrame:CGRectMake(2*self.view.frame.size.width / 3, 0, self.view.frame.size.width/ 3, self.view.frame.size.height-64) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView3];
    [self.tableView3 release];
    self.tableView3.delegate=self;
    self.tableView3.dataSource=self;
       self.tableView3.separatorStyle = UITableViewCellSeparatorStyleNone;


}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView== self.tableView1) {
        return self.proArr.count;
    }else if(tableView == self.tableView2)
        return self.cityArr.count;
    else{
        return self.zoneArr.count;
    }

}

//创建cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView ==self.tableView1) {
    static NSString *reuse=@"reuse";
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
    }
    NSMutableDictionary *proDic=self.proArr[indexPath.row];
    cell.textLabel.text=proDic[@"proName"];
    return cell;
    }else if(tableView ==self.tableView2){
        static NSString *cityResuse=@"cityResuse";
        UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:cityResuse];
        if (!cell) {
            cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cityResuse] autorelease];
        }
        NSMutableDictionary *cityDic=self.cityArr[indexPath.row];
        cell.textLabel.text=cityDic[@"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 (tableView == self.tableView1) {
        //先找到当前点击的是哪一个省
        NSMutableDictionary *proDic=self.proArr[indexPath.row];
        //找到市数组
        self.cityArr=proDic[@"cityArr"];
        //刷新
        [self.tableView2 reloadData];
    }
    else if (tableView ==self.tableView2){
        NSMutableDictionary *cityDic=self.cityArr[indexPath.row];
        self.zoneArr=cityDic[@"zoneArr"];
        [self.tableView3 reloadData] ;

    }

}


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

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值