//
// BLSelectCityViewController.swift
// bestnewkjb
//
// Created by on 2019/7/11.
// Copyright © 2019 . All rights reserved.
//
import UIKit
import Alamofire
import SwiftyJSON
class BLOrderCommentsCompleteVC: LLFBaseViewController ,UITableViewDataSource , UITableViewDelegate {
@objc var touchTitleView: (_ titleStr:String) -> Void = {_ in }
@IBOutlet weak var currentLocationLabel: UILabel!
@IBOutlet weak var topView: UIView!
@IBOutlet weak var tableView: UITableView!
//数据源
var dataArray = NSMutableArray()
let citys = ["北京市", "上海市", "天津市", "长沙", "重庆市", "合肥市", "毫州市", "芜湖市", "马鞍山市", "池州市", "黄山市", "滁州市", "安庆市", "淮南市", "淮北市", "蚌埠市", "巢湖市", "宿州市", "六安市", "阜阳市", "铜陵市", "明光市", "天长市", "宁国市", "界首市", "桐城市", "广州市", "韶关市", "深圳市", "珠海市", "汕头市", "佛山市", "江门市", "湛江市", "茂名市", "肇庆市", "惠州市", "梅州市", "汕尾市", "河源市", "阳江市", "清远市", "东莞市", "中山市", "潮州市", "揭阳市", "云浮市", "昆明市", "曲靖市", "玉溪市", "保山市", "昭通市", "丽江市", "思茅市", "临沧市", "楚雄彝族自治州", "红河哈尼族彝族自治州", "文山壮族苗族自治州", "西双版纳傣族自治州", "大理白族自治州", "德宏傣族景颇族自治州", "怒江傈僳族自治州", "迪庆藏族自治州"]
//分别用来存储分组好的城市和每个组的标题,cityGroups变量是一个字典,每个key都是城市名的第一个字母,对应的值是以这个key开头的城市
var cityGroups = [String: [String]]()
var groupTitles = [String]()
//MARK: - private methods
// MARK: - 获取首字母(传入汉字字符串, 返回大写拼音首字母)
func getFirstLetterFromString(aString: String) -> (String) {
let mutableString = NSMutableString.init(string: aString)
CFStringTransform(mutableString as CFMutableString, nil, kCFStringTransformToLatin, false)
let pinyinString = mutableString.folding(options: String.CompareOptions.diacriticInsensitive, locale: NSLocale.current)
let strPinYin = polyphoneStringHandle(string: aString, pinyinString: pinyinString).uppercased()
let firstString = strPinYin.prefix(1)
let regexA = "^[A-Z]$"
let predA = NSPredicate.init(format: "SELF MATCHES %@", regexA)
return predA.evaluate(with: firstString) ? String(firstString) : "#"
}
// 多音字处理
func polyphoneStringHandle(string:String, pinyinString:String) -> String {
if string.hasPrefix("长") {return "chang"}
if string.hasPrefix("沈") {return "shen"}
if string.hasPrefix("厦") {return "xia"}
if string.hasPrefix("地") {return "di"}
if string.hasPrefix("重") {return "chong"}
return pinyinString;
}
//MARK:- 按照城市字母顺序排序
func makeCityToGroup() {
// 遍历citys数组中的所有城市
for city in citys {
// 拿到首字母作为key
let firstLetter = getFirstLetterFromString(aString: city as String)
// 检查是否有firstLetter对应的分组存在, 有的话直接把city添加到对应的分组中
// 没有的话, 新建一个以firstLetter为key的分组
if var value = cityGroups[firstLetter] {
value.append(city)
cityGroups[firstLetter] = value
}
else {
cityGroups[firstLetter] = [city]
}
}
//拿到所有的key将它排序, 作为每个组的标题
groupTitles = cityGroups.keys.sorted()
}
//MARK: - Life Cycle
override func viewDidLoad() {
super.viewDidLoad()
self.title = "城市选择"
//拿到数据源
self .makeCityToGroup()
tableView.sectionIndexColor = UIColor.black //索引字体颜色
tableView.sectionIndexBackgroundColor = UIColor.clear
tableView.delegate = self
tableView.dataSource = self
tableView.register(UINib.init(nibName: "BLSelectCityCell", bundle: nil), forCellReuseIdentifier: "BLSelectCityCell")
}
deinit {
print(#file,"----------------------------------销毁了")
}
//MARK: - UITableViewDataSource
func numberOfSections(in tableView: UITableView) -> Int {
return self.cityGroups.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let firstLetter = groupTitles[section]
return cityGroups[firstLetter]!.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:BLSelectCityCell = tableView.dequeueReusableCell(withIdentifier: "BLSelectCityCell", for: indexPath) as! BLSelectCityCell
let firstLetter = groupTitles[indexPath.section]
let citysInAGroup = cityGroups[firstLetter]!
cell.cityLabel.text = citysInAGroup[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return groupTitles[section]
}
func sectionIndexTitles(for tableView: UITableView) -> [String]? {
return groupTitles
}
//MARK: - UITableViewDelegate
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
self.touchTitleView("梅州")
self.navigationController?.popViewController(animated: true)
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0.01
}
}
网友评论