感觉省市区要被玩坏了……….
#import "MainViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface MainViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic,retain)NSMutableArray *proArr;
@property(nonatomic, retain)UITableView *proTableView;
@property(nonatomic, retain)UITableView *cityTableView;
@property(nonatomic, retain)UITableView *zoneTableView;
@property(nonatomic, retain)NSMutableArray *cityarr;
@property(nonatomic, retain)NSMutableArray *zonearr;
@end
@implementation MainViewController
- (void)dealloc{
[_proArr release];
[_proTableView release];
[_cityTableView release];
[_zoneTableView release];
[_cityarr release];
[_zonearr release];
[super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[self creat];
}
return self;
}
- (void)creat{
NSString *path = @"/Users/dllo/Desktop/UI 学习/UI09 _ 多种TableView/UI09 _ 多种TableView/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:@"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"];
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)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// 如果设置成不透明,navigation就有毛玻璃的效果.
//automaticallyAdjustsScrollViewInsets 这条属性的作用是:当只有一个TableView时,会将tableView的坐标原点从(0,64)开始,但有多个时,将不管其他tableView,就会造成tableView不对其的情况发生.解决方法,1.把半透明改成不透明2.还可以将这条属性关闭,即所有的tableView都从(0,0)开始.再将纵坐标+64即可.这样nagation就有毛玻璃效果了.
self.automaticallyAdjustsScrollViewInsets = NO;
self.navigationController.navigationBar.translucent = NO;
self.proTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH/3, HEIGHT - 64) style:UITableViewStylePlain];
self.proTableView.backgroundColor =[UIColor orangeColor];
[self.view addSubview:self.proTableView];
[_proTableView release];
self.cityTableView = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH/3, 0, WIDTH/3, HEIGHT - 64) style:UITableViewStylePlain];
// cell的横线
self.cityTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.cityTableView.backgroundColor = [UIColor cyanColor];
[self.view addSubview:self.cityTableView];
[_cityTableView release];
self.zoneTableView = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH/3*2, 0, WIDTH/3, HEIGHT - 64) style:UITableViewStylePlain];
self.zoneTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.zoneTableView.backgroundColor = [UIColor redColor];
[self.view addSubview:self.zoneTableView];
[_zoneTableView release];
//
self.proTableView.delegate = self;
self.cityTableView.delegate = self;
self.zoneTableView.delegate = self;
self.proTableView.dataSource = self;
self.cityTableView.dataSource = self;
self.zoneTableView.dataSource = self;
}
- (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) {
static NSString *proreuse = @"proreuse";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:proreuse];
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:proreuse] autorelease];
}
NSMutableDictionary *prodic = self.proArr[indexPath.row];
cell.textLabel.text = prodic[@"proname"];
cell.textLabel.font = [UIFont systemFontOfSize:18];
return cell;
}else if (tableView == self.cityTableView){
static NSString *cityreuse = @"reuse";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cityreuse] ;
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cityreuse] autorelease];
}
NSMutableDictionary *citydic = self.cityarr[indexPath.row];
cell.textLabel.text = citydic[@"cityname"];
cell.textLabel.font = [UIFont systemFontOfSize:16];
return cell;
}else{
static NSString *zonereuse = @"reuse";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:zonereuse] ;
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:zonereuse] autorelease];
}
cell.textLabel.text = self.zonearr[indexPath.row];
cell.textLabel.font = [UIFont systemFontOfSize:14];
return cell;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// 先判断当前是哪一个tableView被点击
if (tableView == self.proTableView) {
// 先找到当前点击的是哪一个省
self.cityarr = self.proArr[indexPath.row][@"cityarr"];
//对市的tableView进行reloadData
[self.cityTableView reloadData];
self.zonearr = [NSMutableArray array];
[self.zoneTableView reloadData];
}else if (tableView == self.cityTableView){
self.zonearr = self.cityarr[indexPath.row][@"zonearr"];
[self.zoneTableView reloadData];
}
}
效果图如下:
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-06 14:51:32