UI 09 tableView 中国省市区. 一个页面, 三个tableView

感觉省市区要被玩坏了……….

#import "MainViewController.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)UITableView *proTableView;
@property(nonatomic, retain)UITableView *cityTableView;
@property(nonatomic, retain)UITableView *zoneTableView;
@property(nonatomic, retain)NSMutableArray *cityarr;
@property(nonatomic, retain)NSMutableArray *zonearr;


@end

@implementation MainViewController
- (void)dealloc{
    [_proArr release];
    [_proTableView release];
    [_cityTableView release];
    [_zoneTableView release];
    [_cityarr release];
    [_zonearr release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self creat];
    }
    return  self;
}

- (void)creat{
    NSString *path = @"/Users/dllo/Desktop/UI 学习/UI09 _ 多种TableView/UI09 _ 多种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 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];
        }
    }

}

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

    // 如果设置成不透明,navigation就有毛玻璃的效果.
    //automaticallyAdjustsScrollViewInsets 这条属性的作用是:当只有一个TableView时,会将tableView的坐标原点从(0,64)开始,但有多个时,将不管其他tableView,就会造成tableView不对其的情况发生.解决方法,1.把半透明改成不透明2.还可以将这条属性关闭,即所有的tableView都从(0,0)开始.再将纵坐标+64即可.这样nagation就有毛玻璃效果了.
    self.automaticallyAdjustsScrollViewInsets = NO;

    self.navigationController.navigationBar.translucent = NO;

    self.proTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH/3, HEIGHT - 64) style:UITableViewStylePlain];
    self.proTableView.backgroundColor =[UIColor orangeColor];
    [self.view addSubview:self.proTableView];
    [_proTableView release];

    self.cityTableView = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH/3, 0, WIDTH/3, HEIGHT - 64) style:UITableViewStylePlain];

    // cell的横线
    self.cityTableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    self.cityTableView.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:self.cityTableView];
    [_cityTableView release];

    self.zoneTableView = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH/3*2, 0, WIDTH/3, HEIGHT - 64) style:UITableViewStylePlain];
    self.zoneTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.zoneTableView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.zoneTableView];
    [_zoneTableView release];
//
    self.proTableView.delegate = self;
    self.cityTableView.delegate = self;
    self.zoneTableView.delegate = self;

    self.proTableView.dataSource = self;
    self.cityTableView.dataSource = self;
    self.zoneTableView.dataSource = self;



}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == self.proTableView) {
        return self.proArr.count;
    }else if(tableView == self.cityTableView){
        return self.cityarr.count;
    }else{
        return self.zonearr.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (tableView == self.proTableView) {
        static NSString *proreuse = @"proreuse";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:proreuse];
        if (!cell) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:proreuse] autorelease];
        }
        NSMutableDictionary *prodic = self.proArr[indexPath.row];
         cell.textLabel.text = prodic[@"proname"];
        cell.textLabel.font = [UIFont systemFontOfSize:18];

        return cell;
    }else if (tableView == self.cityTableView){
        static NSString *cityreuse = @"reuse";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cityreuse] ;
        if (!cell) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cityreuse] autorelease];
        }
        NSMutableDictionary *citydic = self.cityarr[indexPath.row];

        cell.textLabel.text = citydic[@"cityname"];
        cell.textLabel.font = [UIFont systemFontOfSize:16];
        return cell;
    }else{
        static NSString *zonereuse = @"reuse";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:zonereuse] ;
        if (!cell) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:zonereuse] autorelease];
        }
        cell.textLabel.text = self.zonearr[indexPath.row];
        cell.textLabel.font = [UIFont systemFontOfSize:14];
        return cell;
    }

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    // 先判断当前是哪一个tableView被点击
    if (tableView == self.proTableView) {
        // 先找到当前点击的是哪一个省
        self.cityarr = self.proArr[indexPath.row][@"cityarr"];
        //对市的tableView进行reloadData
        [self.cityTableView reloadData];
        self.zonearr = [NSMutableArray array];
        [self.zoneTableView reloadData];
    }else if (tableView == self.cityTableView){
        self.zonearr = self.cityarr[indexPath.row][@"zonearr"];
        [self.zoneTableView reloadData];
    }
}

效果图如下:
这里写图片描述
这里写图片描述
这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值