一个ViewController里有三个TableView,分别为省市区,逐级跳转

准备工作不赘述
MainViewController.m

#import "MainViewController.h"
#import "SecondViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface MainViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)NSMutableArray *proArr;
@property(nonatomic,retain)NSMutableArray *cityArr;
@property(nonatomic,retain)NSMutableArray *zoneArr;
@property(nonatomic,retain)UITableView *proTableView;
@property(nonatomic,retain)UITableView *cityTableView;
@property(nonatomic,retain)UITableView *zoneTableView;

@end

@implementation MainViewController
-(void)dealloc
{
    [_proArr release];
    [_cityArr release];
    [_zoneArr release];
    [_proTableView release];
    [_cityTableView release];
    [_zoneTableView 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/UINote/UI 8-TableView省市区字典数组/UI 8-TableView省市区字典数组/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:_cityArr forKey:@"cityArr"];
            // 将省字典放到省数组中r
            [_proArr addObject:proDic];
        }
        else if ([temp hasPrefix:@"  "]&&![temp hasPrefix:@"    "])
        {
            // 找到对应的城市
            // 创建一个市字典
            NSMutableDictionary *cityDic=[NSMutableDictionary dictionary];
            // 将市名放到市字典中
            [cityDic setObject:temp forKey:@"cityName"];
            // 创建一个区数组
            self.zoneArr=[NSMutableArray array];
            // 将区数组添加到市字典中
            [cityDic setObject:_zoneArr forKey:@"zoneArr"];
            // 给市字典找一个位置
            // 市字典在省字典中
            // 省字典在省数组中
            NSMutableDictionary *proDic=[_proArr lastObject];
            _cityArr=proDic[@"cityArr"];
            [_cityArr addObject:cityDic];
        }
        else{
            // 先找到省字典
            NSMutableDictionary *proDic=[_proArr lastObject];
            // 找到市数组
            _cityArr=proDic[@"cityArr"];
            // 找到市字典
            NSMutableDictionary *cityDic=[_cityArr lastObject];
            // 找到区数组
             _zoneArr=cityDic[@"zoneArr"];
            [_zoneArr addObject:temp];
        }
    }

}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor whiteColor];
    self.navigationController.navigationBar.translucent=NO;


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


    // 铺三个tableView
    _proTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH/3, HEIGHT -64) style:UITableViewStyleGrouped];
    [self.view addSubview:_proTableView];
    _proTableView.dataSource=self;
    _proTableView.delegate=self;
    [_proTableView release];


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


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


}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView ==_proTableView ) {
        return _proArr.count;
    }
    else if (tableView ==_cityTableView) {
        return _cityArr.count;
    }
    else{
        return _zoneArr.count;
    }
}

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

    if (tableView==_proTableView) {
        static NSString *proReuse=@"proReuse";
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:proReuse];
        if (!cell) {
            cell =[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:proReuse] autorelease];
        }
        NSMutableDictionary *proDic=_proArr[indexPath.row];
        cell.textLabel.text=proDic[@"proName"];
        return cell;
    }
        else if (tableView==_cityTableView)
        {
        static NSString *cityReuse=@"cityReuse";
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cityReuse];
        if (!cell) {
            cell =[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cityReuse]autorelease];
            }
            NSMutableDictionary *cityDic=_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:UITableViewCellStyleDefault reuseIdentifier:zoneReuse]autorelease];
            }
            cell.textLabel.text=_zoneArr[indexPath.row];
            return cell;
        }

}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
 // 判断当前是哪一个tableView被点击
    if(tableView ==_proTableView){
        // 找到当前点击的省
        NSMutableDictionary *dic=self.proArr[indexPath.row];
        _cityArr=dic[@"cityArr"];
        // 对市的tableView进行reloaddata
        [_cityTableView reloadData];
    }
    if(tableView ==_cityTableView){
        NSMutableDictionary *cityDic=self.cityArr[indexPath.row];
        _zoneArr=cityDic[@"zoneArr"];
        [_zoneTableView reloadData];
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值