UI總結(jié)-多個tableView
如何在一個頁面鋪多個tableView,下面是對省市區(qū)(數(shù)據(jù)的格式是.TXT)的羅列,分別在三個tableView上顯示,點擊省顯示對應省的市,點擊市顯示對應市的區(qū),來看代碼吧:
#import "ViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface ViewController ()<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 ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor whiteColor];
[self creatView];
[self creatData];
}
-(void)creatView{
//automaticallyAdjustsScrollViewInsets根據(jù)按所在界面的status bar,navigationbar,與tabbar的高度,自動調(diào)整scrollview的 inset,設置為no,不讓viewController調(diào)整,我們自己修改布局即可
self.automaticallyAdjustsScrollViewInsets = YES;
//省對應的tableView
self.proTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH / 3, HEIGHT) style:UITableViewStylePlain];
self.proTableView.dataSource = self;
self.proTableView.delegate = self;
[self.view addSubview:self.proTableView];
[self.proTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
//城市對應的tableView
self.cityTableView = [[UITableView alloc]initWithFrame:CGRectMake(WIDTH / 3, 64, WIDTH / 3 , HEIGHT - 64) style:UITableViewStylePlain];
self.cityTableView.dataSource = self;
self.cityTableView.delegate = self;
[self.view addSubview:self.cityTableView];
[self.cityTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
//區(qū)對應的tableView
self.zoneTableView = [[UITableView alloc]initWithFrame:CGRectMake(WIDTH / 3 * 2, 64, WIDTH / 3, HEIGHT - 64) style:UITableViewStylePlain];
self.zoneTableView.dataSource = self;
self.zoneTableView.delegate = self;
[self.view addSubview:self.zoneTableView];
[self.zoneTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
}
-(void)creatData{
//接受數(shù)據(jù)
//找文件的路徑
NSString *path = @"/Users/dllo/Desktop/UI總結(jié)/多個tableView的練習/多個tableView的練習/area.txt";
//將.txt文件的內(nèi)容全部賦值給字符串str
NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
//把str字符串按換行截成字符串賦值給數(shù)組
NSArray *strArr = [str componentsSeparatedByString:@"\n"];
self.proArr = [NSMutableArray array];
//對數(shù)組進行遍歷
for (NSString *temp in strArr) {
//判斷該行字符串是否是有一個空格開頭
if (![temp hasPrefix:@" "]) {
NSMutableDictionary *proDic = [NSMutableDictionary dictionary];
NSMutableArray *cityArr = [NSMutableArray array];
[proDic setObject:temp forKey:@"proName"];
[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)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//點擊方法:點擊省的tableView,對應省的市的tableView加載
//點擊市的tableView,對應市的區(qū)的tableView加載
if (tableView == self.proTableView) {
self.cityArr = self.proArr[indexPath.row][@"cityArr"];
[self.cityTableView reloadData];
}else if(tableView == self.cityTableView){
self.zoneArr = self.cityArr[indexPath.row][@"zoneArr"];
[self.zoneTableView reloadData];
}
}
-(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) {
UITableViewCell *cell = [self.proTableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
cell.textLabel.text = self.proArr[indexPath.row][@"proName"];
return cell;
}else if (tableView == self.cityTableView){
UITableViewCell *cell = [self.cityTableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class]) forIndexPath:indexPath];
cell.textLabel.text = self.cityArr[indexPath.row][@"cityName"];
return cell;
}else {
UITableViewCell *cell = [self.zoneTableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class]) forIndexPath:indexPath];
cell.textLabel.text = self.zoneArr[indexPath.row];
return cell;
}
}
運行結(jié)果如下圖:

1.gif