iOS UITableView , UITableViewController ,UITableViewCell實現全國各省市遍曆,選擇相應的地區,uitableview遍曆cell

來源:互聯網
上載者:User

iOS UITableView , UITableViewController ,UITableViewCell實現全國各省市遍曆,選擇相應的地區,uitableview遍曆cell

我們先看一下效果

                 

代碼如下

首先是第一個頁面 

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檔案結構

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.