支持国内所有主城市地区。实现了闭包回传,边上城市头字母选择跳转。
强行模块化,强迫症节奏。本来好好在plist读取,看着不爽,非要放在一个swift文件。
下面读取数据算法有点粗糙,导致读取数据没有那么流畅。以后有空再更改下。
(づ ̄3 ̄)づ╭❤~
iOS开发 - 国内城市选择控制器② - 支持搜索
CityViewController.Swift
import UIKit
class CityViewController: UIViewController {
//闭包回传
var selectCityBack: ((cityName: String) -> Void)?
//MARK: - Property 属性
var tableView:UITableView!
let screen_W = UIScreen.mainScreen().bounds.size.width
let screen_H = UIScreen.mainScreen().bounds.size.height
let cityModel = CityDataModel()
//MARK: - LifeCycle 生命周期
override func viewDidLoad() {
super.viewDidLoad()
setupNavigationBar()
setupTableView()
}
//MARK: - ViewSetup 视图设置
func setupNavigationBar() {
let navBarView = UIView(frame: CGRect(x: 0, y: 0, width: screen_W, height: 64))
navBarView.backgroundColor = UIColor.whiteColor()
self.view.addSubview(navBarView)
let closeBtn = UIButton(type: .Custom)
closeBtn.frame = CGRect(x: 20, y: 30, width: 40, height: 20)
closeBtn.setTitle("关闭", forState: .Normal)
closeBtn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
closeBtn.addTarget(self, action: #selector(CityViewController.closeButton), forControlEvents: .TouchUpInside)
navBarView.addSubview(closeBtn)
let titleLabel = UILabel(frame: CGRect(x: screen_W/2 - 50, y: 30, width: 100, height: 25))
titleLabel.textColor = UIColor.blackColor()
titleLabel.textAlignment = .Center
titleLabel.font = UIFont.systemFontOfSize(15)
titleLabel.text = "选择城市"
navBarView.addSubview(titleLabel)
}
func setupTableView() {
tableView = UITableView(frame: CGRect(x: 0, y: 64, width: screen_W, height: screen_H - 64), style: .Plain)
self.view.addSubview(tableView)
tableView.delegate = self
tableView.sectionIndexColor = UIColor.orangeColor()
tableView.dataSource = self
}
//MARK: - private 内部方法
func closeButton() {
self.dismissViewControllerAnimated(true, completion: nil)
}
}
//MARK: - Delegate
extension CityViewController: UITableViewDelegate, UITableViewDataSource {
// UITableViewDataSource
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return cityModel.dictK.count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let cityhead = cityModel.dictK[section]
return (cityModel.dictKY[cityhead]?.count)!
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return cityModel.dictK[section]
}
func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
return cityModel.dictK
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 49
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell")
if cell == nil {
cell = UITableViewCell(style: .Default, reuseIdentifier: "Cell")
}
let cityhead = cityModel.dictK[indexPath.section]
let cityPinyin = cityModel.dictKY[cityhead]![indexPath.row]
cell?.textLabel?.text = "\(CityDataModel.cityDic[cityPinyin]!)"
return cell!
}
// UITableViewDelegate
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cityhead = cityModel.dictK[indexPath.section]
let cityPinyin = cityModel.dictKY[cityhead]![indexPath.row]
let currCtiy = CityDataModel.cityDic[cityPinyin]
self.selectCityBack?(cityName: currCtiy!) //回传数据
self.dismissViewControllerAnimated(true, completion: nil)
print(currCtiy)
}
}
// MARK: - 城市数据
struct CityDataModel {
static let cityDic = ["wuhai": "乌海", "chaoyang": "朝阳", "lanzhou": "兰州", "nanjing": "南京", "wuhan": "武汉", "hegang": "鹤岗", "yongzhou": "永州", "loudi": "娄底", "lianyungang": "连云港", "zhangzhou": "漳州", "zhumadian": "驻马店", "wuxi": "无锡", "chizhou": "池州", "shanghai": "上海", "shaoyang": "邵阳", "qiannan": "黔南", "zhongwei": "中卫", "tacheng": "塔城", "tonghua": "通化", "benxi": "本溪", "jingmen": "荆门", "cixi": "慈溪", "changsha": "长沙", "wanzhou": "万州", "jiyuan": "济源", "yanji": "延吉", "deyang": "德阳", "tangshan": "唐山", "changzhou": "常州", "meizhou": "梅州", "shijiazhuang": "石家庄", "yiwu": "义乌", "changchun": "长春", "linyi": "临沂", "shangyu": "上虞", "heihe": "黑河", "shannan": "山南", "fuxin": "阜新", "puyang": "濮阳", "changde": "常德", "changdu": "昌都", "ruian": "瑞安", "lijiang": "丽江", "wuzhou": "梧州", "tongliao": "通辽", "huaihua": "怀化", "ningbo": "宁波", "puer": "普洱", "kelamayi": "克拉玛依", "hanzhong": "汉中", "wuwei": "武威", "binzhou": "滨州", "guilin": "桂林", "nantong": "南通", "yongkang": "永康", "xiantao": "仙桃", "kezhou": "克州", "maoming": "茂名", "shuangyashan": "双鸭山", "changle": "长乐", "liyang": "溧阳", "suining": "遂宁", "huanggang": "黄冈", "huidong": "惠东", "honghe": "红河", "wulumuqi": "乌鲁木齐", "yangjiang": "阳江", "hainanzhou": "海南州", "hangzhou": "杭州", "jiangning": "江宁", "xuchang": "许昌", "tanggu": "塘沽", "zhanjiang": "湛江", "xuancheng": "宣城", "danyang": "丹阳", "chengde": "承德", "longyan": "龙岩", "sanxia": "三峡", "qiqihaer": "齐齐哈尔", "beijing": "北京", "huaian": "淮安", "jixi": "鸡西", "guiyang": "贵阳", "zhenjiang": "镇江", "nanchang": "南昌", "yaan": "雅安", "panzhihua": "攀枝花", "changxing": "长兴", "langfang": "廊坊", "guyuan": "固原", "mudanjiang": "牡丹江", "fuyang": "阜阳", "nanning": "南宁", "yili": "伊犁", "baotou": "包头", "dongying": "东营", "cangzhou": "沧州", "shantou": "汕头", "zhangjiajie": "张家界", "taiyuan": "太原", "jincheng": "晋城", "ali": "阿里", "qiandongnan": "黔东南", "yushu": "玉树", "dongguan": "东莞", "sanya": "三亚", "kunming": "昆明", "zhaoqing": "肇庆", "guangan": "广安", "daxinganling": "大兴安岭", "dali": "大理", "wuzhong": "吴忠", "xiaoshan": "萧山", "huizhou": "惠州", "jieyang": "揭阳", "luoyang": "洛阳", "baishan": "白山", "handan": "邯郸", "xingtai": "邢台", "jinan": "济南", "dandong": "丹东", "songyuan": "松原", "fuzhoufz": "抚州", "heze": "菏泽", "sanmenxia": "三门峡", "panjin": "盘锦", "yanan": "延安", "qingyuan": "清远", "aletai": "阿勒泰", "liaoyang": "辽阳", "shuozhou": "朔州", "qianxinan": "黔西南", "huadu": "花都", "yingkou": "营口", "haidong": "海东", "jiujiang": "九江", "suihua": "绥化", "kaifeng": "开封", "liuzhou": "柳州", "chuzhou": "滁州", "ankang": "安康", "shenzhen": "深圳", "yanbian": "延边", "zhangjiakou": "张家口", "bazhong": "巴中", "yixing": "宜兴", "mianyang": "绵阳", "hechi": "河池", "aba": "阿坝", "jian": "吉安", "yancheng": "盐城", "quanzhou": "泉州", "bengbu": "蚌埠", "chongzuo": "崇左", "dazhou": "达州", "baise": "百色", "panyu": "番禺", "luzhou": "泸州", "weinan": "渭南", "zigong": "自贡", "wulanchabu": "乌兰察布", "yantai": "烟台", "fushun": "抚顺", "haixi": "海西", "xianggang": "香港", "huaibei": "淮北", "huiyang": "惠阳", "guangzhou": "广州", "foshan": "佛山", "alashanmeng": "阿拉善", "qianan": "迁安", "shanwei": "汕尾", "yueyang": "岳阳", "zhongshan": "中山", "shizuishan": "石嘴山", "xiamen": "厦门", "boertala": "博尔塔拉", "xinzhou": "忻州", "hebi": "鹤壁", "tongchuan": "铜川", "zhoushan": "舟山", "emeishan": "峨眉山", "zhuozhou": "涿州", "lvliang": "吕梁", "qujing": "曲靖", "liaocheng": "聊城", "bijiediqu": "毕节", "huangshan": "黄山", "fuqing": "福清", "yangshuo": "阳朔", "suzhousz": "宿州", "tongling": "铜陵", "dehong": "德宏", "chifeng": "赤峰", "xiangtan": "湘潭", "weihai": "威海", "zhaotong": "昭通", "jinjiang": "晋江", "ningde": "宁德", "changzhi": "长治", "qinhuangdao": "秦皇岛", "luohe": "漯河", "haibei": "海北", "jiangmen": "江门", "bayannaoer": "巴彦淖尔", "qionghai": "琼海", "pingxiang": "萍乡", "jinhua": "金华", "lishui": "丽水", "chengdu": "成都", "anqing": "安庆", "xining": "西宁", "putian": "莆田", "linfen": "临汾", "huludao": "葫芦岛", "shenyang": "沈阳", "tongrendiqu": "铜仁", "linxia": "临夏", "zaozhuang": "枣庄", "qingdao": "青岛", "ezhou": "鄂州", "baoshan": "保山", "fuling": "涪陵", "xinyang": "信阳", "qitaihe": "七台河", "shihezi": "石河子", "dunhunag": "敦煌", "liuan": "六安", "linhai": "临海", "shaoxing": "绍兴", "daqing": "大庆", "changji": "昌吉", "anyang": "安阳", "deqing": "德清", "haerbin": "哈尔滨", "nanping": "南平", "shangqiu": "商丘", "jinchang": "金昌", "haining": "海宁", "zhangye": "张掖", "chenzhou": "郴州", "dongyang": "东阳", "chaozhou": "潮州", "zhangjiagang": "张家港", "xinyu": "新余", "huangnan": "黄南", "linzhi": "林芝", "longnan": "陇南", "hulunbeier": "呼伦贝尔", "naqu": "那曲", "jingzhou": "荆州", "yuyao": "余姚", "hezhou": "贺州", "shunde": "顺德", "laibin": "来宾", "meishan": "眉山", "baoji": "宝鸡", "leshan": "乐山", "suizhou": "随州", "tianjin": "天津", "yinchuan": "银川", "tieling": "铁岭", "laiwu": "莱芜", "chuxiong": "楚雄", "diqing": "迪庆", "haikou": "海口", "pingliang": "平凉", "siping": "四平", "ganzi": "甘孜", "hengyang": "衡阳", "xinganmeng": "兴安", "qinzhou": "钦州", "dingxi": "定西", "dezhou": "德州", "jiamusi": "佳木斯", "yulin": "玉林", "wenshan": "文山", "gannan": "甘南", "jiayuguan": "嘉峪关", "xiaogan": "孝感", "zhoukou": "周口", "yangzhou": "扬州", "wenzhou": "温州", "heyuan": "河源", "fangchenggang": "防城港", "hengshui": "衡水", "taizhoutz": "台州", "jiaozuo": "焦作", "beihai": "北海", "fuzhou": "福州", "lincang": "临沧", "aomen": "澳门", "shaoguan": "韶关", "yibin": "宜宾", "jinzhong": "晋中", "yingtan": "鹰潭", "nanyang": "南阳", "liupanshui": "六盘水", "qingyang": "庆阳", "jinzhou": "锦州", "liangshan": "凉山", "zhuzhou": "株洲", "datong": "大同", "leqing": "乐清", "anshan": "鞍山", "wenling": "温岭", "chaohu": "巢湖", "zunyi": "遵义", "shishi": "石狮", "xiangyang": "襄阳", "nujiang": "怒江", "baicheng": "白城", "wuan": "武安", "fuyangfy": "富阳", "wuhu": "芜湖", "hetian": "和田", "kunshan": "昆山", "yiyang": "益阳", "zhangqiu": "章丘", "guoluo": "果洛", "huangdao": "黄岛", "shiyan": "十堰", "hami": "哈密", "yichunyc": "伊春", "zhuhai": "珠海", "tulufan": "吐鲁番", "zibo": "淄博", "jiaxing": "嘉兴", "nanchong": "南充", "yichang": "宜昌", "yanzhou": "兖州", "taizhou": "泰州", "yichun": "宜春", "dalian": "大连", "xilinguolemeng": "锡林郭勒", "weifang": "潍坊", "guangyuan": "广元", "lasa": "拉萨", "chongqing": "重庆", "neijiang": "内江", "changshu": "常熟", "suzhou": "苏州", "eerduosi": "鄂尔多斯", "jining": "济宁", "yulinyl": "榆林", "maanshan": "马鞍山", "bozhou": "亳州", "rizhao": "日照", "yunfu": "云浮", "huainan": "淮南", "jilin": "吉林", "tongxiang": "桐乡", "shangrao": "上饶", "huangshi": "黄石", "ganzhou": "赣州", "quzhou": "衢州", "shangluo": "商洛", "kashi": "喀什", "wuyishan": "武夷山", "pingdingshan": "平顶山", "jingdezhen": "景德镇", "anshun": "安顺", "xian": "西安", "ziyang": "资阳", "sanming": "三明", "guigang": "贵港", "taicang": "太仓", "xianning": "咸宁", "huhehaote": "呼和浩特", "xishuangbanna": "西双版纳", "taian": "泰安", "rikaze": "日喀则", "xiangxi": "湘西", "baiyin": "白银", "jintan": "金坛", "baoding": "保定", "hefei": "合肥", "bazhou": "巴州", "jiangyin": "江阴", "xianyang": "咸阳", "jiuquan": "酒泉", "enshi": "恩施", "yuxi": "玉溪", "taibei": "台北", "xinxiang": "新乡", "xuzhou": "徐州", "conghua": "从化", "akesu": "阿克苏", "yangquan": "阳泉", "suqian": "宿迁", "zhengzhou": "郑州", "liaoyuan": "辽源", "huzhou": "湖州", "zhuji": "诸暨", "tianshui": "天水", "yuncheng": "运城"]
var dictKY = [String:[String]]()
var dictK:[String] {
return Array(dictKY.keys).sort(<)
}
private var sortdekey:[String] {
let cityDic2 = CityDataModel.cityDic
return Array(cityDic2.keys).sort(<)
}
private var currStrHead = "a"
private var currStrArr = [String]()
init() {
for i in 0..<sortdekey.count {
let currStr = sortdekey[i]
let start = currStr.startIndex
let currStartStr = String(currStr[start])
if currStrHead == currStartStr {
currStrArr.append(currStr)
} else {
let k = currStrHead
let v = currStrArr
dictKY[k] = v
currStrArr.removeAll()
currStrHead = currStartStr
}
}
}
}
使用例子:
let vc = CityViewController()
self.presentViewController(vc, animated: true, completion: nil)
vc.selectCityBack = ({(cityName: String) -> Void in
//self.label.text = cityName
})