三个tableView的联动,分为两部分(1.省市区数组的数据解析)(2.判断是哪个tableView,显示不同的cell)
需要在点击方法里获取需要操作的数组
因为是三个tableView,只有一个tableView可以适应高度,其他的需要设置.两种办法1.将导航控制栏的透明度变成NO.但是需要在高度的基础上-64
// 这种设置,需要在高度的基础上-64
// self.navigationController.navigationBar.translucent = NO;
第2种方法:内嵌的方式(只有第一个tableView适配) 但是有一个属性,只会让第一个滚动视图适应屏幕,从第二个开始就不管了,要是想保留半透明的效果,要把属性设置成NO,然后再设置滚动视图的坐标位置(将纵坐标+64,高度-64)关键词auto
// 内嵌的方式(只有第一个tableView适配)
// 这个属性只会让第一个滚动视图适应屏幕,从第二个开始就不管了,要是想保留半透明的效果了把属性设置成NO,然后再设置滚动视图的坐标位置
// 记得将 纵坐标+64,然后将 高度-64
self.automaticallyAdjustsScrollViewInsets = NO;
获取数据,省市区字典(省数组中放省字典,省字典放第一对key:省名 value:省的字符串 ; 第二对key:市数组 value:市数组 市数组中放市字典, 第一对key:市名 value:市的字符串 第二对key:区数组 value:区数组, 区数组中放字符串就可以)
- (void)createData{
NSString *path = @"/Users/dllo/Desktop/课程/OC/area.txt";
NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSArray *strArr = [str componentsSeparatedByString:@"\n"];
// NSLog(@"%@", strArr);
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"];
<h2> // 将数组加入到字典的时候,需要注意</h2> 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];
}
}
}
tableView的协议中,需要判断具体是哪个tableView 两种方法1.通过tag值和协议方法和tableView对象进行比较(tableView.tag == 1000) 2.控件写成了属性,然后判断地址是否相同(self.proTableView == tableView)
1.点击方法,先判断tableView,然后获取内容,最后不要忘记刷新内存!!!!!!!!!
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// 需要判断点击的是哪一个tableView
// 1.通过tag值,和协议方法的tableView对象进行比较
// 2.控件写成属性,然后判断地址是否相同
// 用tag值判断(tableView.tag == 1000)
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];
}
}
2.判断行数的协议方法,判断tableView,不同的tableView对应不同的数组数
- (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;
}
}
3.cell的协议方法,判断,取值就可以了
- (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:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
}
NSMutableDictionary *dic = self.proArr[indexPath.row];
cell.textLabel.text = dic[@"proName"];
return cell;
} else if (self.cityTableView == tableView){
static NSString *reuse = @"city";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
}
cell.textLabel.text = self.cityArr[indexPath.row][@"cityName"];
return cell;
} else {
static NSString *reuse = @"zone";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
}
//
cell.textLabel.text = self.zoneArr[indexPath.row];
return cell;
}
}