UI总结-多个tableView
2016-05-30 本文已影响190人
Dear丶Musk
UI总结-多个tableView
如何在一个页面铺多个tableView,下面是对省市区(数据的格式是.TXT)的罗列,分别在三个tableView上显示,点击省显示对应省的市,点击市显示对应市的区,来看代码吧:
#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根据按所在界面的status bar,navigationbar,与tabbar的高度,自动调整scrollview的 inset,设置为no,不让viewController调整,我们自己修改布局即可
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])];
//区对应的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{
//接受数据
//找文件的路径
NSString *path = @"/Users/dllo/Desktop/UI总结/多个tableView的练习/多个tableView的练习/area.txt";
//将.txt文件的内容全部赋值给字符串str
NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
//把str字符串按换行截成字符串赋值给数组
NSArray *strArr = [str componentsSeparatedByString:@"\n"];
self.proArr = [NSMutableArray array];
//对数组进行遍历
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,对应市的区的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;
}
}