IOS uses UITableView to select province and city from plist
Recently, a weather application needs to be used to select a city. Considering that city information is not subject to major changes, the xml file of Chinese city information is found on the Internet. The following is a code that uses tableview to implement region selection. This is relatively simple and will not be explained.
AddressViewController. h file
/// AddressViewController. h // AddressDemo /// Created by worthy. zhang on 15/5/29. // Copyright (c) 2015 worthy. zhang. all rights reserved. // # import
# Define kDisplayProvince 0 # define kDisplayCity 1 # define kDisplayArea 2 @ interface AddressViewController: UIViewController
@ Property (nonatomic, strong) UITableView * tableView; @ property (nonatomic, assign) int displayType; @ property (nonatomic, strong) NSArray * provinces; @ property (nonatomic, strong) NSArray * citys; @ property (nonatomic, strong) NSArray * areas; @ property (nonatomic, strong) NSString * selectedProvince; // The selected province @ property (nonatomic, strong) NSString * selectedCity; // The selected city @ property (nonatomic, strong) NSString * selectedArea; // The selected area @ end
AddressViewController. m file
/// AddressViewController. m // AddressDemo /// Created by worthy. zhang on 15/5/29. // Copyright (c) 2015 worthy. zhang. all rights reserved. // # import AddressViewController. h @ interface AddressViewController () @ property (nonatomic, strong) NSIndexPath * selectedIndexPath; // the currently selected NSIndexPath @ end @ implementation AddressViewController-(void) viewDidLoad {[self configureData]; [self configureViews];}-(void) confi GureData {if (self. displayType = kDisplayProvince) {// read the address dictionary from the file NSString * addressPath = [[NSBundle mainBundle] pathForResource: @ address ofType: @ plist]; NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithContentsOfFile: addressPath]; self. provinces = [dict objectForKey: @ address] ;}}-(void) configureViews {if (self. displayType = kDisplayProvince) {// only the cancel button self is displayed on the select province page. navigationItem. LeftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle: @ cancel style: UIBarButtonItemStylePlain target: self action: @ selector (cancel)];} if (self. displayType = kDisplayArea) {// The OK button is displayed only on the select area page. navigationItem. rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle: @ OK style: UIBarButtonItemStylePlain target: self action: @ selector (submit)];} CGRect frame = [self. view bounds]; self. tableView = [[UITableView alloc] initWithFrame: frame]; self. tableView. delegate = self; self. tableView. dataSource = self; [self. view addSubview: self. tableView];}-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {if (self. displayType = kDisplayProvince) {return self. provinces. count;} else if (self. displayType = kDisplayCity) {return self. citys. count;} else {return self. are As. count ;}}- (UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {static NSString * ID = @ cityCell; UITableViewCell * cell = [tableView preview: ID]; if (! Cell) {cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: ID]; if (self. displayType = kDisplayArea) {cell. accessoryType = UITableViewCellAccessoryNone;} else {cell. accessoryType = UITableViewCellAccessoryDisclosureIndicator;} if (self. displayType = kDisplayProvince) {NSDictionary * province = self. provinces [indexPath. row]; NSString * provinceName = [province objectForKey: @ name]; cell. textLabel. text = provinceName;} else if (self. displayType = kDisplayCity) {NSDictionary * city = self. citys [indexPath. row]; NSString * cityName = [city objectForKey: @ name]; cell. textLabel. text = cityName;} else {cell. textLabel. text = self. areas [indexPath. row]; cell. imageView. image = [UIImage imageNamed: @ unchecked];} return cell;}-(void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {if (self. displayType = kDisplayProvince) {NSDictionary * province = self. provinces [indexPath. row]; NSArray * citys = [province objectForKey: @ sub]; self. selectedProvince = [province objectForKey: @ name]; // construct the next-level view controller AddressViewController * cityVC = [[AddressViewController alloc] init]; cityVC. displayType = kDisplayCity; // The display mode is cityVC. citys = citys; cityVC. selectedProvince = self. selectedProvince; [self. navigationController pushViewController: cityVC animated: YES];} else if (self. displayType = kDisplayCity) {NSDictionary * city = self. citys [indexPath. row]; self. selectedCity = [city objectForKey: @ name]; NSArray * areas = [city objectForKey: @ sub]; // construct the AddressViewController * areaVC = [[AddressViewController alloc] init]; areaVC. displayType = kDisplayArea; // The display mode is region areaVC. areas = areas; areaVC. selectedCity = self. selectedCity; areaVC. selectedProvince = self. selectedProvince; [self. navigationController pushViewController: areaVC animated: YES];} else {// cancel the last selected status UITableViewCell * oldCell = [tableView cellForRowAtIndexPath: self. selectedIndexPath]; oldCell. imageView. image = [UIImage imageNamed: @ unchecked]; // check the currently selected status UITableViewCell * newCell = [tableView cellForRowAtIndexPath: indexPath]; newCell. imageView. image = [UIImage imageNamed: @ checked]; // save self. selectedArea = self. areas [indexPath. row]; self. selectedIndexPath = indexPath;}-(void) submit {NSString * msg = [NSString stringWithFormat: @ % @-% @, self. selectedProvince, self. selectedCity, self. selectedArea]; UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ select Address message: msg delegate: nil cancelButtonTitle: @ cancel failed: nil, nil]; [alert show];} -(void) cancel {[self dismissViewControllerAnimated: YES completion: nil];} @ end
As follows: