您的位置:首页 > 其它

三个tableView联动

2015-12-22 19:46 239 查看
#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: