带分区的省市区字典数组
来源:互联网 发布:股票网络销售话术 编辑:程序博客网 时间:2024/05/20 16:01
准备工作不再赘述
一个tableView TitleForHeader为省名,分区个数为省的个数,每个分区中的cell.textLabel.text为省对应的市名
MainViewController.m
#import "MainViewController.h"#import "SecondViewController.h"@interface MainViewController ()<UITableViewDataSource,UITableViewDelegate>@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]; [super dealloc];}#warning 如果在初始化方法里使用self.view,此时还没有创建self.view,系统会自动调用loadView,创建一个self.view,从而改变VC的运行流程,所以我们只在初始化方法里初始化容器等数据部分,不创建视图-(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/UINote/UI 10-带分区的省市区/UI 10-带分区的省市区/area(1).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"]; // 创建一个市数组 self.cityArr=[NSMutableArray array]; // 将市数组添加到省字典中 [proDic setObject:self.cityArr forKey:@"cityArr"]; // 将省字典放到省数组中 [self.proArr addObject:proDic]; } else if ([temp hasPrefix:@" "]&&![temp hasPrefix:@" "]) { // 找到对应的城市 // 创建一个市字典 NSMutableDictionary *cityDic=[NSMutableDictionary dictionary]; // 将市名放到市字典中 [cityDic setObject:temp forKey:@"cityName"]; // 创建一个区数组 self.zoneArr=[NSMutableArray array]; // 将区数组添加到市字典中 [cityDic setObject:self.zoneArr forKey:@"zoneArr"]; // 给市字典找一个位置 // 市字典在省字典中 // 省字典在省数组中 NSMutableDictionary *proDic=[self.proArr lastObject]; self.cityArr=proDic[@"cityArr"]; [self.cityArr addObject:cityDic]; } else{ // 先找到省字典 NSMutableDictionary *proDic=[self.proArr lastObject]; // 找到市数组 self.cityArr=proDic[@"cityArr"]; // 找到市字典 NSMutableDictionary *cityDic=[self.cityArr lastObject]; // 找到区数组 self.zoneArr=cityDic[@"zoneArr"]; [self.zoneArr addObject:temp]; } }}- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor=[UIColor yellowColor]; self.navigationController.navigationBar.translucent=NO; // 铺一个tableView UITableView *tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-64) style:UITableViewStylePlain]; [self.view addSubview:tableView]; tableView.dataSource=self; tableView.delegate=self; [tableView release];}-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return self.proArr.count;}-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ return self.proArr[section][@"proName"];}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ for (int i = 0; i < self.proArr.count; i ++) { if (section == i) { NSArray *cityArr = self.proArr[section][@"cityArr"]; return cityArr.count; }} return 0;}-(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]; }// NSMutableArray *cityArr=self.proArr[indexPath.section][@"cityArr"];// cell.textLabel.text=cityArr[indexPath.row][@"cityName"]; cell.textLabel.text=self.proArr[indexPath.section][@"cityArr"][indexPath.row][@"cityName"]; return cell;}-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ UIView *newView=[[[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 30)] autorelease]; newView.backgroundColor=[UIColor redColor]; UILabel *textLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 50, 20)]; [newView addSubview:textLabel]; [textLabel release]; textLabel.text=self.proArr[section][@"proName"]; UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem]; [button setTitle:@"更多" forState:UIControlStateNormal]; [newView addSubview:button]; return newView;}-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ // 找到省字典// NSMutableDictionary *proDic = self.proArr[indexPath.row];// //省字典对应的市数组// self.cityArr = proDic[@"cityArr"]; SecondViewController *secVC=[[SecondViewController alloc]init]; [self.navigationController pushViewController:secVC animated:YES];// secVC.arr = self.cityArr; [secVC release];}
0 0
- 带分区的省市区字典数组
- 带分区的省市区字典
- 带分区的省市区
- 带分区的UITableView的创建(省市区字典)
- UITableView中带分区的省市区字典,或一个页面出现全部省市区
- UI0_带分区的省市区
- UI10_带分区的省市区
- iOS UI10_带分区的省市区
- UI 10 带分区的省市区......
- 字典+数组的应用——省市区
- OC_省市区字典数组
- 省市区字典数组
- OC-省市区字典数组
- 省市区字典数组
- 省市区字典数组
- UI08_tableview省市区字典数组
- UI09_UITableView省市区字典数组
- UI_UITableView省市区字典数组
- Android学习笔记(二)
- 谷歌被墙,如何给谷歌浏览器添加迅雷下载插件
- 第7章-01
- 汇编中的所有寄存器类型与作用
- 面试题:正整数序列Q中的每个元素都至少能被正整数a和b中的一个整除,给定a和b,需要计算出Q中的前几项
- 带分区的省市区字典数组
- linux相关术语
- 第7章-02
- hdu 5327 Olympiad(水)
- [Leetcode]Reverse Integer
- Linux守护进程设计
- 产品经理在外包项目时如何踩更少的坑
- [Vijos]P1398 奖学金
- 如何去掉wordpress默认插入图片链接