// // MobileAreaCodeViewController.swift // MTP2_iOS // // Created by Handy_Cao on 2020/2/17. // Copyright © 2020 Muchinfo. All rights reserved. // import UIKit /// 手机区号选择视图容器类 class MobileAreaCodeViewController: BaseViewController { // MARK: - 属性列表 /// 搜索框 @IBOutlet weak var searchBar: UISearchBar! /// 数据展示列表 @IBOutlet weak var tableView: UITableView! /// 数据 var array: NSArray? /// 创建block变量 var popBlock: ((_ String: String) ->())? /// cellIdentifier let cellIdentifier = "MobileAreaCode_Cell" // MARK: - 生命周期相关 override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. /// 数据初始化 initData() } // MARK: - 初始化相关 /// 数据初始化 private func initData() { let path = Bundle.main.path(forResource: "AreaCode", ofType: "plist") array = NSArray(contentsOfFile: path ?? "") } // MARK: - 获取联系人姓名首字母(传入汉字字符串, 返回大写拼音首字母) /// 获取联系人姓名首字母(传入汉字字符串, 返回大写拼音首字母) /// - Parameter aString: aString private func getFirstLetter(_ aString: String) -> String { let mutableString = NSMutableString(string: aString) CFStringTransform((mutableString as CFMutableString), nil, kCFStringTransformToLatin, false) let pinyinString = mutableString.folding(options: .diacriticInsensitive, locale: nil) let array = pinyinString.components(separatedBy: .whitespaces) let results = NSMutableArray(capacity: array.count) for pinyin in array { let result = NSString(string: pinyin).substring(to: 1) results.add(result) } return results.componentsJoined(by: "") } } // MARK: - UISearchBarDelegate extension MobileAreaCodeViewController: UISearchBarDelegate { /// searchBarCancelButtonClicked func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { /// 回到上层视图 self.navigationController?.popViewController(animated: true) } func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { /// 初始化数据 self.initData() if searchText.trimmingCharacters(in: CharacterSet.whitespaces) != "" { let selectArray = NSMutableArray() for dict in array ?? [] { let olderlyArray = (dict as! NSDictionary).allValues.first as! NSArray let selectionDictionary = NSMutableDictionary() let newerArray = NSMutableArray() var sign = false for contentArray in olderlyArray { let areaName = (contentArray as! NSArray)[0] as! String let areaCode = (contentArray as! NSArray)[1] as! String let searchCondition = String(format: "%@;%@;%@", self.getFirstLetter(areaName), areaName, areaCode) if NSString(string: searchCondition).localizedCaseInsensitiveContains(searchText) { newerArray.add(contentArray) sign = true } } if sign { let key = (dict as! NSDictionary).allKeys.first as! String selectionDictionary[key] = newerArray selectArray.add(selectionDictionary) } } array = selectArray } /// 刷新列表数据 tableView.reloadData() } func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { /// 失去第一响应 searchBar.resignFirstResponder() } } // MARK: - UITableViewDelegate, UITableViewDataSource extension MobileAreaCodeViewController: UITableViewDelegate, UITableViewDataSource { func numberOfSections(in tableView: UITableView) -> Int { return array?.count ?? 1 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { let dict = array?[section] as! NSDictionary let contentArray = dict.allValues.first as! NSArray return contentArray.count } func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { let dict = array?[section] as! NSDictionary return " " + ((dict.allKeys.first as? String) ?? "") } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let dict = array?[indexPath.section] as! NSDictionary let contentArray = dict.allValues.first as? NSArray let array = contentArray?[indexPath.row] as? NSArray /// block调用 if let _ = self.popBlock { self.popBlock!(array![1] as! String) } self.navigationController?.popViewController(animated: true) } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) let dict = array?[indexPath.section] as? NSDictionary let contentArray = dict?.allValues.first as? NSArray let array = contentArray?[indexPath.row] as? NSArray cell.textLabel?.text = ((array?[1] as? String) ?? "") + " " + ((array?[0] as? String) ?? "") return cell } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 44.0 } }