#import "MainViewController.h"
@interface MainViewController ()<UITableViewDelegate, UITableViewDataSource>
@property(nonatomic, strong) UITableView *areaTabview;
@property(nonatomic, strong) UITableView *subAreaTabview;
@property(nonatomic, strong) NSArray *areaArray;
@property(nonatomic, strong) NSArray *subAreaArray;
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.title = @"城市级联";
CGFloat with = CGRectGetWidth(self.view.bounds) / 2;
CGFloat height = CGRectGetHeight(self.view.bounds);
self.areaTabview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, with, height) style:UITableViewStylePlain];
self.areaTabview.backgroundColor = [UIColor whiteColor];
self.areaTabview.delegate = self;
self.areaTabview.dataSource = self;
[self.areaTabview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"areaTabview"];
[self.view addSubview:self.areaTabview];
self.subAreaTabview = [[UITableView alloc] initWithFrame:CGRectMake(with, 64, with, height-64) style:UITableViewStylePlain];
self.subAreaTabview.backgroundColor = [UIColor lightGrayColor];
self.subAreaTabview.delegate = self;
self.subAreaTabview.dataSource = self;
[self.subAreaTabview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"subAreaTabview"];
[self.view addSubview:self.subAreaTabview];
}
-(NSArray *)areaArray{
if (!_areaArray){
NSString *path = [[NSBundle mainBundle] pathForResource:@"cities" ofType:@"plist"];
_areaArray = [NSArray arrayWithContentsOfFile:path];
}
return _areaArray;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (tableView == self.areaTabview){
self.subAreaArray = [self.areaArray[indexPath.row] valueForKey:@"cities"];
[self.subAreaTabview scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];
[self.subAreaTabview reloadData];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (tableView == self.areaTabview){
return self.areaArray.count;
}else{
return self.subAreaArray.count;
}
}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell;
if (tableView == self.areaTabview){
cell = [tableView dequeueReusableCellWithIdentifier:@"areaTabview"];
cell.textLabel.text = [self.areaArray[indexPath.row] valueForKey:@"name"] ;
}else{
cell = [tableView dequeueReusableCellWithIdentifier:@"subAreaTabview"];
cell.backgroundColor = [UIColor lightGrayColor];
cell.textLabel.text = self.subAreaArray[indexPath.row];
}
return cell;
}
@end
版权声明:本文为zhouzhijin0908原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。