UI10_3个tableView的省市区联动
2015-09-29 08:42
543 查看
AppDelegate.h
AppDelegate.m
RootViewController.h
RootViewController.m
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end
AppDelegate.m
#import "AppDelegate.h" #import "RootViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (void)dealloc { [_window release]; [super dealloc]; } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; [_window release]; RootViewController *rootVC = [[RootViewController alloc] init]; UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:rootVC]; self.window.rootViewController = naVC; [rootVC release]; [naVC release]; return YES; }
RootViewController.h
#import <UIKit/UIKit.h> @interface RootViewController : UIViewController @end
RootViewController.m
#import "RootViewController.h" #define WIDTH self.view.frame.size.width #define HEIGHT self.view.frame.size.height @interface RootViewController ()<UITableViewDataSource, UITableViewDelegate> @property(nonatomic, retain)NSMutableArray *provinceArr; @property(nonatomic, retain)NSMutableArray *cityArr; @property(nonatomic, retain)NSMutableArray *zoneArr; @property(nonatomic, retain)UITableView *provinceTableView; @property(nonatomic, retain)UITableView *cityTableView; @property(nonatomic, retain)UITableView *zoneTableView; @end @implementation RootViewController - (void)dealloc { [_provinceArr release]; [_cityArr release]; [_zoneArr release]; [_provinceTableView release]; [_cityTableView release]; [_zoneTableView release]; [super dealloc]; } - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { [self createData]; } return self; } - (void)createData { NSString *path = @"/Users/dllo/Desktop/青山流云/UI/UI10/UI10_3个tableView省市区联动/UI10_3个tableView省市区联动/area(省市区版).txt"; NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; NSArray *strArr = [str componentsSeparatedByString:@"\n"]; self.provinceArr = [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:@"cityName"]; [self.provinceArr 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:@"zoneName"]; NSMutableDictionary *proDic = self.provinceArr.lastObject; NSMutableArray *cityArr = proDic[@"cityName"]; [cityArr addObject:cityDic]; } else { NSMutableDictionary *proDic = self.provinceArr.lastObject; NSMutableArray *cityArr = proDic[@"cityName"]; NSMutableDictionary *cityDic = cityArr.lastObject; NSMutableArray *zoneArr = cityDic[@"zoneName"]; [zoneArr addObject:temp]; } } } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // automaticallyAdjustsScrollViewInsets会把第一个滚动视图的坐标起始位置设置成(0, 64), 但是它不会管后面的滚动视图, 所以想要保存半透明效果需要把这个属性设置成NO, 然后坐标起始位置都加64 // 或者把导航栏设置成不透明, 坐标系就稳定了 self.automaticallyAdjustsScrollViewInsets = NO; // self.navigationController.navigationBar.translucent = NO; self.provinceTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, WIDTH / 3, HEIGHT - 64) style:UITableViewStylePlain]; [self.view addSubview:self.provinceTableView]; [self.provinceTableView release]; self.provinceTableView.dataSource = self; self.provinceTableView.delegate = self; self.cityTableView = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH / 3, 64, WIDTH / 3, HEIGHT - 64) style:UITableViewStylePlain]; [self.view addSubview:self.cityTableView]; [self.cityTableView release]; self.cityTableView.dataSource = self; self.cityTableView.delegate = self; self.zoneTableView = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH / 3 * 2, 64, WIDTH / 3, HEIGHT - 64) style:UITableViewStylePlain]; [self.view addSubview:self.zoneTableView]; [self.zoneTableView release]; self.zoneTableView.dataSource = self; self.zoneTableView.delegate = self; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // 点击省出现市 if (tableView == self.provinceTableView) { // 再次选择省时, 区消失 // 用[self.zoneArr removeAllObjects];有bug, 再次访问时, 变成了空,,改变了数据源 self.zoneArr = nil; [self.zoneTableView reloadData]; // 把省对应的市数组给属性赋值 self.cityArr = self.provinceArr[indexPath.row] [@"cityName"]; [self.cityTableView reloadData]; } else if (tableView == self.cityTableView) { self.zoneArr = self.cityArr[indexPath.row][@"zoneName"]; [self.zoneTableView reloadData]; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *Reuse = @"Reuse"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Reuse]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Reuse]; } if (tableView == self.provinceTableView) { cell.textLabel.text = self.provinceArr[indexPath.row][@"proName"]; } else if (tableView == self.cityTableView) { cell.textLabel.text = self.cityArr[indexPath.row][@"cityName"]; } else { cell.textLabel.text = self.zoneArr[indexPath.row]; } return cell; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (tableView == self.provinceTableView) { return self.provinceArr.count; } else if (tableView == self.cityTableView){ return self.cityArr.count; } else { return self.zoneArr.count; } }
相关文章推荐
- UI10_UITableViewCell自适应高度
- UI_导航视图控制器
- drupal markdown editor for BUEditor
- UGUI学习笔记1——Canvas
- NGUI之按钮置灰
- HDU 3177 Crixalis's Equipment 贪心
- [转自codeforces] How to come up with the solutions: techniques
- Druid 导致图标字体文件不能加载的问题
- UITextField基础属性
- UITextField Delegate 键盘弹起
- 隐藏UIScrollView的滚动条的方法
- UIButton的一些技巧
- UIButton 技巧setTitleEdgeInsets
- UIPickerView 使用
- IOS UIAlertView
- CAS:PKIX path building failed
- Dynamic Web Module 3.0 requires Java 1.6 or newer
- UINavigationControl笔记
- 【UIKit-110-7】#import <UIKit/UITableView.h>提前注册
- 【UIKit-110-6】#import <UIKit/UITableView.h>外观