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

来源:http://www.cnblogs.com/qianLL/archive/2016/03/17/5289706.html
-Advertisement-
Play Games

我們先看一下效果 代碼如下 首先是第一個頁面 rootTableViewController.h rootTableViewController.m 第二個頁面 cityTableViewController.h 第二個頁面 cityTableViewController.m 指定根目錄AppDel


我們先看一下效果

                 

代碼如下

首先是第一個頁面 

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文件結構


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 功能實現:一個EditView 一個撥打按鈕,輸入號碼跳轉到撥號界面 界面佈局:activity_call.xml CallActivity的Create方法 增加撥打電話的許可權:AndroidManifest.xml 至此可以實現撥號功能。
  • 分類:C#、Android、VS2015 創建日期:2016-03-18 一、卸載原來安裝的Xamarin for VS 4.0.0.1717版 下麵是Xamarin for VS發佈的版本簡介: ……更早的版本(3.11.XXX )略 2015年11月發佈:Xamarin for VS 4.0.0...
  • 1、開始界面 2、開啟新的activity代碼 3、獲取聯繫人 1)清單文件 <uses-permission android:name="android.permission.READ_CONTACTS"/>//許可權 2)通過內容提供者獲取聯繫人 4、設置聯繫人進Listview java代碼:
  • 本篇主要講述百度地圖的導航功能: 第一步:在使用百度導航之前,我們需要在百度地圖開放平臺上下載導航的 SDK,共85.8M,網速不好的同學可提前準備好。 第二步:引入導航所需的系統包 將AudioToolbox.framework、ImageIO.framework、CoreMotion.frame
  • 1.SQLite資料庫: SQLite 是一個開源的嵌入式關係資料庫,實現自包容、零配置、支持事務的SQL資料庫引擎。 其特點是高度便攜、使 用方便、結構緊湊、高效、可靠。 與其他資料庫管理系統不同,SQLite 的安裝和運行非常簡單,在大多數情況下 - 只要確保 SQLite的二進位文件存在即可開
  • 一般白色就是0,黑色就是1 單色點陣圖: 24位點陣圖 256色 安卓中預設使用32位的 將一個圖片放在SD卡上,使用BitmapFactory.decodeFile解析得Bitmap設置ImageView顯示 以上的圖片可以正常載入 異常現象 載入 的圖片過大,如2560*1520之類的,載入 時就會
  • 本文主要實現了微信的個人主頁的設置: 目錄文件如下: 實現代碼如下: RootTableViewController.h RootTableViewController.m AppDelegate.h AppDelegate.m 效果圖如下:
  • 主要在清單文件這樣配置: 2、那麼如何讓屏幕橫屏或者豎屏時對activity沒有影響呢?應當這樣配置:
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...