省市区选择器显示一般通过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