您的位置:首页 > 其它

一个页面创建多个tableView 相关联(省市区数组)

2015-08-12 10:21 417 查看

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

1.创建三个数组的属性

代码:

@property(nonatomic,retain)NSMutableArray *proArr;
@property(nonatomic ,retain)NSMutableArray *cityArr;
@property(nonatomic ,retain)NSMutableArray *zoneArr;
三个tableView的属性
@property(nonatomic,retain)UITableView *proTableView;
@property(nonatomic ,retain)UITableView *cityTableView;
@property(nonatomic ,retain)UITableView *zoneTableView;


注意:如果在初始化方法里使用self.view,此时还没有创建self.view,系统会自动调用loadView,创建一个self.view,从而改变VC的运行流程,所以我们只在初始化方法里初始化容器的数据部分,不创建视图

2.初始化并解析字典:

代码:

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[self creatData];
}
return self;
}

-(void)creatData{
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 setValue:temp forKey:@"proName"];
NSMutableArray *cityArr=[NSMutableArray array];
[proDic setValue: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"];
NSMutableArray *cityArr=[[self.proArr lastObject] objectForKey:@"cityArr"];
[cityArr addObject:cityDic];

}else{
NSMutableArray *zoneArr =[[[[self.proArr lastObject] objectForKey:@"cityArr"] lastObject] objectForKey:@"zoneArr"];
[zoneArr addObject:temp];
}
}
}


3.创建tableView

不要忘记代理人的设置和签订协议

设置背景颜色

self.view.backgroundColor=[UIColor yellowColor];


取消透明度

self.navigationController.navigationBar.translucent=NO;


省的tableView

代码:

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


创建市的tableView

self.cityTableView =[[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.cityTableView];

[_cityTableView release];

self.cityTableView.delegate =self;

self.cityTableView.dataSource=self;

创建区的tableView

self.zoneTableView =[[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.zoneTableView];
[_zoneTableView release];
self.zoneTableView.delegate =self;
self.zoneTableView.dataSource =self;


4.必须出发的协议(1)返回行数

代码:

-(NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (tableView == self.proTableView) {
NSLog(@"省,proArr.count = %ld",self.proArr.count);
return self.proArr.count;
}
else if (tableView == self.cityTableView){
//
NSLog(@"市,cityArr.count = %ld",self.cityArr.count);
return self.cityArr.count;
}else{
NSLog(@"区,zoneArr.count = %ld",self.zoneArr.count);
return self.zoneArr.count;
}

}


5.必须触发的协议(2)创建cell

代码:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (tableView == self.proTableView) {

NSLog(@"sheng");
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][@"proName"];
return cell;
}else if(tableView == self.cityTableView){

NSLog(@"shi");
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{

NSLog(@"qu");
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;

}
}


6,点击触发协议

代码:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    self.clickIndexPath =indexPath;
//    [self.cityTableView reloadData];
// 当前是哪一个tableView被点击
if(tableView ==self.proTableView){
// 先找到是哪个省
//        NSMutableDictionary *proDic =self.proArr[indexPath.row];
self.cityArr=self.proArr[indexPath.row][@"cityArr"];
//对市数组进行reloaddate
[self.cityTableView reloadData];
//        self.zoneArr =nil;
//        [self.zoneTableView reloadData];
self.zoneArr=nil;
[self.zoneTableView reloadData];
}else if (tableView == self.cityTableView){
// 找市字典,再找区数组
self.zoneArr =self.cityArr[indexPath.row][@"zoneArr"];
// 对区数组进行刷新
[self.zoneTableView reloadData];

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息