省市区选择器显示一般通过PickerView 和 TableView来实现,数据通过plist属性文件,sqlite,或是文本文件实现。
我的这个是通过txt文本本件实现的。
首先对txt文件进行解析,把解析的数据放到数组中。
然后,用通过pickerView或tableView控件显示就ok了。
PickerView实现的效果:
TableView实现的效果:
核心代码:

#import "ProvinceViewController.h" #import "CityViewController.h" @interface ProvinceViewController () @end @implementation ProvinceViewController - (void)loadData{ NSString *filePath = [[NSBundle mainBundle] pathForResource:@"area" ofType:@"txt"]; NSString *areaString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; NSArray *areasArray = [areaString componentsSeparatedByString:@"\n"]; _aArray = [NSMutableArray array]; for (NSString *string in areasArray) { if (![string hasPrefix:@" "]) { NSMutableArray *cities = [NSMutableArray array]; string = [self cutStringForSuffixBlank:string]; NSDictionary *province = [NSDictionary dictionaryWithObjectsAndKeys:string,@"pName",cities,@"cities", nil]; [_aArray addObject:province]; continue; } if ([string hasPrefix:@" "] && ![string hasPrefix:@" "]) { NSDictionary *province = [_aArray lastObject]; NSMutableArray *cities = [province objectForKey:@"cities"]; NSMutableArray *areas = [NSMutableArray array]; string = [self cutStringForPrefixBlank:string]; string = [self cutStringForSuffixBlank:string]; NSDictionary *city = [NSDictionary dictionaryWithObjectsAndKeys:string,@"cName",areas,@"areas", nil]; [cities addObject:city]; continue; } NSDictionary *city = [[[_aArray lastObject] objectForKey:@"cities"] lastObject]; NSMutableArray *areas = [city objectForKey:@"areas"]; string = [self cutStringForPrefixBlank:string]; string = [self cutStringForSuffixBlank:string]; [areas addObject:string]; } _areaData = [[AreaData alloc] initWithDataArray:_aArray]; } - (NSString *)cutStringForPrefixBlank:(NSString *)string{ for (int i = 0; i < 5; i++) { if ([string hasPrefix:@" "]) { string = [string substringFromIndex:1]; } } return string; } - (NSString *)cutStringForSuffixBlank:(NSString *)string{ int index = 0; for (int i = 0; i < [string length]; i++) { unichar c = [string characterAtIndex:i]; if (c == ' ') { NSLog(@"%d",i); index = i; } } NSString *stringResult = [string substringToIndex:index]; return stringResult; } - (void)createControl{ CGRect rect = CGRectZero; rect.size.width = self.view.bounds.size.width; rect.size.height = self.view.bounds.size.height - 44.0; UITableView *tableView = [[UITableView alloc] initWithFrame:rect]; [tableView setDataSource:self]; [tableView setDelegate:self]; [self.view addSubview:tableView]; } - (void)viewDidLoad { [super viewDidLoad]; [self loadData]; [self createControl]; } - (void)dealloc{ [_areaData release]; [super dealloc]; } # pragma mark -- UITableViewDataSource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [_areaData provinces]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *cellIdentifier = @"cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; } [cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton]; [cell.textLabel setText:[_areaData province:indexPath.row]]; return cell; } # pragma mark -- UITableViewDelegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ CityViewController *city = [[[CityViewController alloc] init] autorelease]; [city setTitle:@"市"]; [city setAreaData:_areaData]; [city setIndexForProvince:indexPath.row]; [self.navigationController pushViewController:city animated:YES]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
致力于ios开发
分类:
ios小案例
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库