星空网 > 软件开发 > 操作系统

iOS UITableView , UITableViewController ,UITableViewCell实现全国各省市遍历,选择相应的地区

我们先看一下效果

iOS UITableView , UITableViewController ,UITableViewCell实现全国各省市遍历,选择相应的地区images/loading.gif' data-original="http://images2015.cnblogs.com/blog/818241/201603/818241-20160317220434928-1981469475.png" />                 iOS UITableView , UITableViewController ,UITableViewCell实现全国各省市遍历,选择相应的地区

代码如下

首先是第一个页面 

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];      //读取文件    self.arr= [NSArray arrayWithContentsOfFile: [[NSBundle mainBundle]pathForResource:@"area.plist" ofType:nil]];  //设置标题  self.title=@"省";  //  NSLog(@"%@",self.arr);  //设置tableView 读取信息  [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 - 每个分组的里面的个数- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {  return self.arr.count;}#pragma mark - 设置显示信息- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];      cell.textLabel.text=self.arr[indexPath.row][@"State"];  return cell;}#pragma mark - 选中行的信息-(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 - 行高-(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

第二个页面 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=@"地级市";    //接收选择行的行数,及其数组  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.  //返回上一页  self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"back" style:2 target:self action:@selector(backpage)];}-(void)backpage{  [self.navigationController popToRootViewControllerAnimated:YES];}- (void)didReceiveMemoryWarning {  [super didReceiveMemoryWarning];    }#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 dequeueReusableCellWithIdentifier:@"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:@"提示" message:[NSString stringWithFormat:@"您确定要选择%@%@",self.city[self.rows][@"State"],self.array[indexPath.row]] preferredStyle:UIAlertControllerStyleAlert];      UIAlertAction *cancle=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];    UIAlertAction *ok=[UIAlertAction actionWithTitle:@"确定" 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

指定根目录AppDelegate.h

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

指定根目录AppDelegate.m

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

 这个页面的关键点就在于,在他选中的时候,要把选中的行数,和集合传到页面二中

-(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];

}

 


这个有点类似于页面跳转的属性传值,只要能传递要页面二中,后面的就好处理了

注:plist文件结构

iOS UITableView , UITableViewController ,UITableViewCell实现全国各省市遍历,选择相应的地区




原标题:iOS UITableView , UITableViewController ,UITableViewCell实现全国各省市遍历,选择相应的地区

关键词:IOS

IOS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

深鹏发物流:https://www.goluckyvip.com/tag/40876.html
深圳amazon培训:https://www.goluckyvip.com/tag/40877.html
深圳fba:https://www.goluckyvip.com/tag/40879.html
店铺申诉:https://www.goluckyvip.com/tag/4088.html
深圳fba到美国:https://www.goluckyvip.com/tag/40880.html
深圳fba发货:https://www.goluckyvip.com/tag/40881.html
长治婚庆女司仪和主持人:https://www.vstour.cn/a/366176.html
北京丰台区水上乐园哪家好玩?:https://www.vstour.cn/a/366177.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流