iOS使用UITableView從plist中選擇省市區

來源:互聯網
上載者:User

iOS使用UITableView從plist中選擇省市區

最近在做一個天氣應用,需要使用者選擇所在城市。考慮到城市資訊一般不會發生大的變化,所以從網上找到了中國城市資訊的xml檔案,下面是利用一個tableview實現地區選擇的代碼,比較簡單,就不解釋了。
AddressViewController.h檔案

////  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;//選中的省@property(nonatomic,strong)NSString *selectedCity;//選中的市@property(nonatomic,strong)NSString *selectedArea;//選中的區@end

AddressViewController.m檔案

////  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;//當前選中的NSIndexPath@end@implementation AddressViewController-(void)viewDidLoad{    [self configureData];    [self configureViews];}-(void)configureData{    if (self.displayType == kDisplayProvince) {        //從檔案讀取地址字典        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) { //只在選擇省份頁面顯示取消按鈕        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@取消 style:UIBarButtonItemStylePlain target:self action:@selector(cancel)];    }    if (self.displayType == kDisplayArea) {//只在選擇地區頁面顯示確定按鈕        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@確定 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.areas.count;    }}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString* ID = @cityCell;    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: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];        //構建下一級視圖控制器        AddressViewController *cityVC = [[AddressViewController alloc]init];        cityVC.displayType = kDisplayCity;//顯示模式為城市        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];        //構建下一級視圖控制器        AddressViewController *areaVC = [[AddressViewController alloc]init];        areaVC.displayType = kDisplayArea;//顯示模式為地區        areaVC.areas = areas;        areaVC.selectedCity = self.selectedCity;        areaVC.selectedProvince = self.selectedProvince;        [self.navigationController pushViewController:areaVC animated:YES];    }    else{        //取消上一次選定狀態        UITableViewCell *oldCell =  [tableView cellForRowAtIndexPath:self.selectedIndexPath];        oldCell.imageView.image = [UIImage imageNamed:@unchecked];        //勾選當前選定狀態        UITableViewCell *newCell =  [tableView cellForRowAtIndexPath:indexPath];        newCell.imageView.image = [UIImage imageNamed:@checked];        //儲存        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:@選擇地址 message:msg delegate:nil cancelButtonTitle:@取消 otherButtonTitles:nil, nil];    [alert show];}-(void)cancel{    [self dismissViewControllerAnimated:YES completion:nil];}@end

如下:


 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.