31.多种tableView和带分区的省市区显示
2015-08-11 20:34
260 查看
1.个人觉得本文的重心是将省/市/区三个数组都设置为属性, 然后省数组直接通过数据解析拿到, 后面的市和区数组在被点击省和市后确定是那个数组, 这样对有些地方取得数组里的元素的个数是很方便的, 不需要通过字典一层一层的解析拿到数组;
#import "MainViewController.h" @interface MainViewController ()<UITableViewDataSource,UITableViewDelegate> //三个tableView作为属性 @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; @end @implementation MainViewController - (void)dealloc { [_proArr release]; [_cityArr release]; [_zoneArr release]; [_proTableView release]; [_cityTableView release]; [_zoneTableView release]; [super dealloc]; } - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { [self createData]; } return self; } - (void)createData{ NSString *path = [[NSBundle mainBundle] pathForResource:@"area" ofType:@"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 alloc] init]; [proDic setObject:temp forKey:@"proName"]; NSMutableArray *cityArr = [[NSMutableArray alloc] init]; [proDic setObject:cityArr forKey:@"cityArr"]; [self.proArr addObject:proDic]; } else if (![temp hasPrefix:@" "] && [temp hasPrefix:@" "]){ NSMutableDictionary *zoneDic = [[NSMutableDictionary alloc] init]; [zoneDic setObject:temp forKey:@"cityName"]; NSMutableArray *zoneArr = [[NSMutableArray alloc] init]; [zoneDic setObject:zoneArr forKey:@"zoneArr"]; NSMutableDictionary *proDic = [self.proArr lastObject]; NSMutableArray *cityArr = proDic[@"cityArr"]; [cityArr addObject:zoneDic]; } else{ NSMutableDictionary *proDic = [self.proArr lastObject]; NSMutableArray *cityArr = proDic[@"cityArr"]; NSMutableDictionary *zoneDic = [cityArr lastObject]; NSMutableArray *zoneArr = zoneDic[@"zoneArr"]; [zoneArr addObject:temp]; } } }
- (void)viewDidLoad { [super viewDidLoad]; self.title = @"省市区"; self.view.backgroundColor = [UIColor whiteColor]; self.navigationController.navigationBar.translucent = NO; //automaticallyAdjustsScrollViewInsets这条属性当视图里只有一个滚动视图的时候,他会自动把坐标改为(0,64),如果有多个,那么后面的它使不会管的,所以也就会造成tableView对不齐的问题,解决方法一:把半透明改成不透明,二:把属性关掉,然后我们自己tableView全体y轴+64,能保留半透明效果 //self.automaticallyAdjustsScrollViewInsets = NO; //一个页面里有三个tableView,进行互相的联动,点击省--->市--->区 self.proTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 100, 603) style:UITableViewStylePlain]; self.proTableView.delegate = self; self.proTableView.dataSource = self; self.proTableView.rowHeight = 50; self.proTableView.separatorStyle = UITableViewCellSeparatorStyleNone; self.proTableView.separatorColor = [UIColor cyanColor]; [self.view addSubview:self.proTableView]; [_proTableView release]; self.cityTableView = [[UITableView alloc] initWithFrame:CGRectMake(80, 0, 150, 603) style:UITableViewStylePlain]; self.cityTableView.delegate = self; self.cityTableView.dataSource = self; self.cityTableView.rowHeight = 50; self.cityTableView.separatorStyle = UITableViewCellSeparatorStyleNone; self.cityTableView.separatorColor = [UIColor cyanColor]; [self.view addSubview:self.cityTableView]; [_cityTableView release]; self.zoneTableView = [[UITableView alloc] initWithFrame:CGRectMake(200, 0, 200, 603) style:UITableViewStylePlain]; self.zoneTableView.delegate = self; self.zoneTableView.dataSource = self; self.zoneTableView.rowHeight = 50; self.zoneTableView.separatorStyle = UITableViewCellSeparatorStyleNone; self.zoneTableView.separatorColor = [UIColor cyanColor]; [self.view addSubview:self.zoneTableView]; [_zoneTableView release]; }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ if (tableView == self.proTableView) { return self.proArr.count; } if (tableView == self.cityTableView) { return self.cityArr.count; } if (tableView == self.zoneTableView) { return self.zoneArr.count; } return 0; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ if (tableView == self.proTableView) { static NSString *ID = @"proName"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; } NSDictionary *dic = self.proArr[indexPath.row]; cell.textLabel.text = dic[@"proName"]; return cell; } if (tableView == self.cityTableView) { static NSString *ID = @"cityName"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; } NSMutableDictionary *cityDic = self.cityArr[indexPath.row]; cell.textLabel.text = cityDic[@"cityName"]; return cell; } if (tableView == self.zoneTableView) { static NSString *ID = @"zoneName"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; } cell.textLabel.text = self.zoneArr[indexPath.row]; return cell; } return nil; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ //当前是那一个省 if (tableView == self.proTableView) { //先找到当前点击的是哪一个省 NSDictionary *proDic = self.proArr[indexPath.row]; self.cityArr = proDic[@"cityArr"]; [self.cityTableView reloadData]; //当重新选择一个省的时候让后面的区列表清空 self.zoneArr = nil; [self.zoneTableView reloadData]; } if (tableView == self.cityTableView) { NSDictionary *cityDic = self.cityArr[indexPath.row]; self.zoneArr = cityDic[@"zoneArr"]; [self.zoneTableView reloadData]; } }
简单截图
2.带分区的省市区显示
#import "MainViewController.h" @interface MainViewController ()<UITableViewDataSource,UITableViewDelegate> @property(nonatomic,retain)NSMutableArray *proArr; @end @implementation MainViewController #warning 如果在初始化方法里使用self.view,此时还没有创建self.view,系统会自动调用loadview,创建一个self.view,从而改变vc的运行的流程,所以我们只在初始化方法里初始容器等数据部分,不创建视图 //下面者两个方法和上面的一模一样 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { [self createData]; } return self; } - (void)createData{ NSString *path = [[NSBundle mainBundle] pathForResource:@"area" ofType:@"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 alloc] init]; [proDic setObject:temp forKey:@"proName"]; NSMutableArray *cityArr = [[NSMutableArray alloc] init]; [proDic setObject:cityArr forKey:@"cityArr"]; [self.proArr addObject:proDic]; } else if (![temp hasPrefix:@" "] && [temp hasPrefix:@" "]){ NSMutableDictionary *zoneDic = [[NSMutableDictionary alloc] init]; [zoneDic setObject:temp forKey:@"cityName"]; NSMutableArray *zoneArr = [[NSMutableArray alloc] init]; [zoneDic setObject:zoneArr forKey:@"zoneArr"]; NSMutableDictionary *proDic = [self.proArr lastObject]; NSMutableArray *cityArr = proDic[@"cityArr"]; [cityArr addObject:zoneDic]; } else{ NSMutableDictionary *proDic = [self.proArr lastObject]; NSMutableArray *cityArr = proDic[@"cityArr"]; NSMutableDictionary *zoneDic = [cityArr lastObject]; NSMutableArray *zoneArr = zoneDic[@"zoneArr"]; [zoneArr addObject:temp]; } } } - (void)viewDidLoad { [super viewDidLoad]; self.navigationController.navigationBar.translucent = NO; UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 603) style:UITableViewStyleGrouped]; tableView.dataSource = self; tableView.delegate = self; [self.view addSubview:tableView]; [tableView release]; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return self.proArr.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ NSArray *arr = self.proArr[section][@"cityArr"]; return arr.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *ID = @"proname"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; } NSArray *arr = self.proArr[indexPath.section][@"cityArr"]; NSDictionary *cityDic = arr[indexPath.row]; cell.textLabel.text = cityDic[@"cityName"]; return cell; } //下面两个方法要用一起使用,有了第一个方法,第二个方法才更加有意义 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ return self.proArr[section][@"proName"]; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ //给头部添加几个自定义视图 UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 20)]autorelease]; view.backgroundColor = [UIColor cyanColor]; //在自定义视图上显示省名 UILabel *letfLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 20)]; [view addSubview:letfLabel]; letfLabel.text = self.proArr[section][@"proName"]; [letfLabel release]; //在右边添加一个更多的按钮 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(300, 0, 100, 20); [button setTitle:@"更多" forState:UIControlStateNormal]; [view addSubview:button]; return view; }
简单截图
相关文章推荐
- 自由堆叠的屋顶——线段树的应用
- 利用【深度网络】高效提取feature
- POI使用汇总
- C++实现堆排序
- JAVA学习1_JAR文件包及jar命令详解
- HDU 5372 Segment Game 树状数组
- SQL Server:“数据收缩”
- 【Cocos2d入门教程五】Cocos2d-x动作篇
- [BZOJ3343] 教主的魔法|分块
- HDU3232 Crossing Rivers 数学期望问题
- MySQL数据库事务隔离级别(Transaction Isolation Level)
- hdoj 1272 小希的迷宫
- machine learning in coding(python):使用贪心搜索【进行特征选择】
- C++简单单例模式
- Android屏幕适配
- 机器学习基础篇:K means
- 【转】在shell中捕捉信号的trap命令
- hdu 5375 Gray code (简单DP)
- 前端学习的那些事儿
- FMDB 排它锁