iOS使用UITableView从plist中选择省市区
来源:互联网 发布:红帽linux是什么 编辑:程序博客网 时间:2024/05/22 13:35
最近在做一个天气应用,需要用户选择所在城市。考虑到城市信息一般不会发生大的变化,所以从网上找到了中国城市信息的xml文件,下面是利用一个tableview实现地区选择的代码,比较简单,就不解释了。
AddressViewController.h文件
//// AddressViewController.h// AddressDemo//// Created by worthy.zhang on 15/5/29.// Copyright (c) 2015年 worthy.zhang. All rights reserved.//#import <UIKit/UIKit.h>#define kDisplayProvince 0#define kDisplayCity 1#define kDisplayArea 2@interface AddressViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>@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
效果图如下:
Demo地址:
https://github.com/WorthyZhang/AddressDemo
省市区plist数据下载地址:
http://download.csdn.net/detail/worthyzhang/8751317
0 0
- iOS使用UITableView从plist中选择省市区
- IOS 中UITableView使用
- ios从.plist文件中加载资源
- IOS中UITableView使用总结
- IOS中UITableView使用总结
- UITableView实现从plist获取数据
- iOS-中国省市区(大陆)对照表plist读取
- iOS-中国省市区plist(续)UIPickerView的应用
- 使用 UITableView 创建表格应用——从plist文件加载并显示数据(转)
- iOS UITableView索引选择
- ios开发从.plist文件中获取数据
- IOS之从plist文件中获取数据
- iOS中UITableView中使用总结
- 全国省市区完整plist
- 【iOS】plist的使用
- ios使用plist
- iOS中使用UITableView加载数据项
- ios中列表UITableView的使用
- IOS开发中的几种设计模式介绍
- leetcode 188---Best Time to Buy and Sell Stock IV
- 基于javacv的数字图像处理
- 什么是准货币,准货币包括哪些
- 唐山哪儿治疗自闭症专业
- iOS使用UITableView从plist中选择省市区
- git
- SQLite的简介
- iOS多线程编程之NSOperation和NSOperationQueue的使用
- 第12周-阅读、修改和运行关于交通工具类的程序,回答问题,以理解相关技术方法
- java基础整理の自动装箱雨拆箱(包装类)
- 第12周项目4-点和圆的关系(2)
- PHP、Java、.Net和Javascript的AES加密解密实现
- yum 安装报 关于Public key for *.rpm is not installed 的解决方法