您的位置:首页 > 移动开发 > Objective-C

Objective-C 省市区 三个tableView联动

2015-12-24 21:09 405 查看
// 所有方法都在根视图下

// 定义属性

@property(nonatomic,retain)UITableView *proTableView;

@property(nonatomic,retain)UITableView *cityTableView;

@property(nonatomic,retain)UITableView *zoneTableView;

@property(nonatomic,retain)NSMutableArray *proArr;

@property(nonatomic,retain)NSMutableArray *cityArr;

@property(nonatomic,retain)NSMutableArray *zoneArr;


self.view.backgroundColor = [UIColor whiteColor];

// 第一种方式: 这个设置,需要在原有基础上-64

self.navigationController.navigationBar.translucent = NO;

// 第二种方式

// 这个属性只会让第一个滚动视图适应屏幕,从第二个开始就不管了,要是想保留半透明效果可以把属性设置成NO,然后再设置滚动视图的坐标位置

// self.automaticallyAdjustsScrollViewInsets = NO;

// 省tableView

self.proTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH / 3, HEIGHT - 64) style:UITableViewStylePlain];

self.proTableView.dataSource = self;

self.proTableView.delegate = self;

self.proTableView.tag = 1000;

[self.view addSubview:self.proTableView];

[_proTableView release];



// 市tableView

self.cityTableView = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH / 3, 0, WIDTH / 3, HEIGHT - 64) style:UITableViewStylePlain];

self.cityTableView.delegate = self;

self.cityTableView.dataSource = self;

self.cityTableView.tag = 1001;

[self.view addSubview:_cityTableView];

[_cityTableView release];

// 区tableView

self.zoneTableView = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH / 3 * 2, 0, WIDTH / 3, HEIGHT - 64) style:UITableViewStylePlain];

// self.zoneTableView.backgroundColor = [UIColor greenColor];

self.zoneTableView.delegate = self;

self.zoneTableView.dataSource = self;

[self.view addSubview:_zoneTableView];

[_zoneTableView release];

// 定义一个方法,遍历省市区

[self createData];

// 省市区遍历


- (void)createData{

NSString *path = @"/Users/dllo/Desktop/资料库/07文件/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 setObject:temp forKey:@"name"];

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"];

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];

}

}

for (NSMutableDictionary *proDic in self.proArr)
{

NSLog(@"%@",proDic[@"name"]);

for (NSMutableDictionary *cityDic in proDic[@"cityArr"])
{

NSLog(@"%@",cityDic[@"cityName"]);

for (NSString *temp in cityDic[@"zoneArr"]) {

NSLog(@"%@",temp);

}

}

}

}



// 判断当前在哪个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;

}

}





// 判断当前在哪个tableView,显示对应的值

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

if (self.proTableView == tableView) {

NSString *reuse = @"reuse";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];

if (!cell) {

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];

}

cell.textLabel.text = self.proArr[indexPath.row][@"name"];

return cell;

}else if (self.cityTableView == tableView){

// 创建市tableview的cell

NSString *reuse = @"reuse";

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{

NSString *reuse = @"reuse";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];

if (!cell) {

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];

}

cell.textLabel.text = self.zoneArr[indexPath.row];

return cell;

}

}



// 点击方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

// 需要判断点击的是哪一个tableview

if (tableView.tag == 1000) {

// 或者用属性地址:self.proTableView == tableView

self.cityArr = self.proArr[indexPath.row][@"cityArr"];

[self.cityTableView reloadData];

// 用来消除一个小bug,可以把这句话注掉试一下

self.zoneArr = NULL;

[self.zoneTableView reloadData];

}else if (tableView.tag == 1001){

self.zoneArr = self.cityArr[indexPath.row][@"zoneArr"];

[self.zoneTableView reloadData];

}

}



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