IOS UITableView, UITableViewController, and UITableViewCell are used to traverse all provinces and cities across the country. Select the corresponding region and uitableview to traverse the cell.

Source: Internet
Author: User

IOS UITableView, UITableViewController, and UITableViewCell are used to traverse all provinces and cities across the country. Select the corresponding region and uitableview to traverse the cell.

Let's take a look at the effect.

The Code is as follows:

The first page

RootTableViewController. h

#import <UIKit/UIKit.h>#import "cityTableViewController.h"@interface rootTableViewController : UITableViewController@property(nonatomic,strong)NSArray *arr;@end

RootTableViewController. m

# Import "rootTableViewController. h "@ interface rootTableViewController () @ end @ implementation rootTableViewController-(void) viewDidLoad {[super viewDidLoad]; // read the file self. arr = [NSArray arrayWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @ "area. plist "ofType: nil]; // set the title self. title = @ "Province"; // NSLog (@ "% @", self. arr); // sets tableView read information [self. tableView registerClass: [UITableViewCell class] forCellReuseIdentifier: @ "reuseIdentifier"]; // Uncomment the following line to preserve selection between presentations. // self. clearsSelectionOnViewWillAppear = NO; // Uncomment the following line to display an Edit button in the navigation bar for this view controller. // self. navigationItem. rightBarButtonItem = self. editButtonItem;}-(void) didReceiveMemoryWarning {[super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated .} # pragma mark-Table view data source-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {return 1 ;}# pragma mark-number of each Group-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {return self. arr. count ;}# pragma mark-set display information-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {UITableViewCell * cell = [tableView finished: @ "reuseIdentifier" forIndexPath: indexPath]; cell. textLabel. text = self. arr [indexPath. row] [@ "State"]; return cell;} # pragma mark-information of the selected row-(void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {cityTableViewController * city = [[cityTableViewController alloc] init]; city. rows = (int) indexPath. row; city. city = self. arr; // NSLog (@ "% d", city. rows); [self. navigationController pushViewController: city animated: YES] ;}# pragma mark-Row Height-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath {return 50 ;} /* // Override to support conditional editing of the table view. -(BOOL) tableView :( UITableView *) tableView canEditRowAtIndexPath :( NSIndexPath *) indexPath {// Return NO if you do not want the specified item to be editable. return YES;} * // Override to support editing the table view. -(void) tableView :( UITableView *) tableView commitEditingStyle :( UITableViewCellEditingStyle) editingStyle forRowAtIndexPath :( NSIndexPath *) indexPath {if (editingStyle = UITableViewCellEditingStyleDelete) {// Delete the row from the data source [tableView deleteRowsAtIndexPaths: @ [indexPath] withRowAnimation: UITableViewRowAnimationFade];} else if (editingStyle = UITableViewCellEditingStyleInsert) {// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view} * // Override to support rearranging the table view. -(void) tableView :( UITableView *) tableView moveRowAtIndexPath :( NSIndexPath *) fromIndexPath toIndexPath :( NSIndexPath *) toIndexPath {} * // Override to support conditional rearranging of the table view. -(BOOL) tableView :( UITableView *) tableView canMoveRowAtIndexPath :( NSIndexPath *) indexPath {// Return NO if you do not want the item to be re-orderable. return YES;} * // * # pragma mark-Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation-(void) prepareForSegue :( UIStoryboardSegue *) segue sender :( id) sender {// Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller .} * // @ end

CityTableViewController. h

#import <UIKit/UIKit.h>@interface cityTableViewController : UITableViewController@property(nonatomic,assign)int rows;@property(nonatomic,strong)NSArray *city;@end

Second page cityTableViewController. m

# Import "cityTableViewController. h "@ interface cityTableViewController () @ property (strong, nonatomic) NSMutableArray * array; @ end @ implementation cityTableViewController-(void) viewDidLoad {[super viewDidLoad]; self. array = [NSMutableArray array]; self. title = @ "prefecture-level city"; // receives the number of rows selected and Its Array for (NSDictionary * city in self. city [self. rows] [@ "Cities"]) {[self. array addObject: city [@ "city"];} // NSLog (@ "% @", self. city [self. rows] [@ "State"]); [self. tableView registerClass: [UITableViewCell class] forCellReuseIdentifier: @ "reuseIdentifier"]; // Uncomment the following line to preserve selection between presentations. // self. clearsSelectionOnViewWillAppear = NO; // Uncomment the following line to display an Edit button in the navigation bar for this view controller. // return to the previous page self. navigationItem. leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle: @ "back" style: 2 target: self action: @ selector (backpage)];}-(void) backpage {[self. navigationController orientation: YES];}-(void) didReceiveMemoryWarning {[super didreceivemorywarning];} # pragma mark-Table view data source-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {return 1;}-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {return self. array. count;}-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {UITableViewCell * cell = [tableView metadata: @ "reuseIdentifier" forIndexPath: indexPath]; cell. textLabel. text = self. array [indexPath. row]; return cell;}-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath {return 50;}-(void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {UIAlertController * message = [UIAlertController alertControllerWithTitle: @ "prompt" message: [NSString stringWithFormat: @ "are you sure you want to select %. city [self. rows] [@ "State"], self. array [indexPath. row] preferredStyle: Regular]; UIAlertAction * cancle = [UIAlertAction actionWithTitle: @ "cancel" style: handle handler: nil]; UIAlertAction * OK = [UIAlertAction actionWithTitle: @ "OK" style: UIAlertActionStyleDefault handler: nil]; [message addAction: OK]; [message addAction: cancle]; [self presentViewController: message animated: YES completion: nil];} /* // Override to support conditional editing of the table view. -(BOOL) tableView :( UITableView *) tableView canEditRowAtIndexPath :( NSIndexPath *) indexPath {// Return NO if you do not want the specified item to be editable. return YES;} * // Override to support editing the table view. -(void) tableView :( UITableView *) tableView commitEditingStyle :( UITableViewCellEditingStyle) editingStyle forRowAtIndexPath :( NSIndexPath *) indexPath {if (editingStyle = UITableViewCellEditingStyleDelete) {// Delete the row from the data source [tableView deleteRowsAtIndexPaths: @ [indexPath] withRowAnimation: UITableViewRowAnimationFade];} else if (editingStyle = UITableViewCellEditingStyleInsert) {// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view} * // Override to support rearranging the table view. -(void) tableView :( UITableView *) tableView moveRowAtIndexPath :( NSIndexPath *) fromIndexPath toIndexPath :( NSIndexPath *) toIndexPath {} * // Override to support conditional rearranging of the table view. -(BOOL) tableView :( UITableView *) tableView canMoveRowAtIndexPath :( NSIndexPath *) indexPath {// Return NO if you do not want the item to be re-orderable. return YES;} * // * # pragma mark-Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation-(void) prepareForSegue :( UIStoryboardSegue *) segue sender :( id) sender {// Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller .} * // @ end

Specify the root directory AppDelegate. h

#import <UIKit/UIKit.h>#import "rootTableViewController.h"@interface AppDelegate : UIResponder <UIApplicationDelegate>@property (strong, nonatomic) UIWindow *window;@end

Specify the root directory AppDelegate. m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    self.window.rootViewController=[[UINavigationController alloc]initWithRootViewController:[[rootTableViewController alloc]initWithStyle:UITableViewStylePlain]];    return YES;}

The key point of this page is that when you select it, you need to upload the selected number of rows and set to the second part of the page.

-(Void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {

CityTableViewController * city = [[cityTableViewController alloc] init];

// Number of rows for receiving

City. rows = (int) indexPath. row;

// Receives the entire set

City. city = self. arr;

// NSLog (@ "% d", city. rows );

[Self. navigationController pushViewController: city animated: YES];

}

 

This is a bit similar to the property transfer value of page Jump. As long as the value can be transferred to page 2, the following will be processed.

Note: plist file structure

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.