class UserViewController: UIViewController ,UITableViewDelegate, UITableViewDataSource{
var tableView:UITableView?
var nameArray : [String] = []
var charArr : [String] = []
var adHeaders : [String] = []
var addressBookDict = [String:[PPPersonModel]]()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
loadDate_table()
for i in 0..
let model : PPPersonModel = PPPersonModel()
model.name = nameArray[i]
// 获取到姓名的大写首字母
let firstLetterString = getFirstLetterFromString(aString: model.name)
if addressBookDict[firstLetterString] != nil {
// swift的字典,如果对应的key在字典中没有,则会新增
addressBookDict[firstLetterString]?.append(model)
} else {
let arrGroupNames = [model]
addressBookDict[firstLetterString] = arrGroupNames
}
}
var nameKeys = Array(addressBookDict.keys).sorted()
// 将 "#" 排列在 A~Z 的后面
if nameKeys.first == "#" {
nameKeys.insert(nameKeys.first!, at: nameKeys.count)
nameKeys.remove(at: 0);
}
adHeaders += adHeaders+nameKeys
print("111111","\(nameKeys) ")
print("adHeaders",adHeaders)
for i in 0..
let key = nameKeys[i]
letmodelArr = addressBookDict[key]
letcount = modelArr!.count
for j in 0..
letmodel:PPPersonModel = modelArr![j]
charArr.append(model.name)
}
}
print("333333",charArr)
//创建表视图
self.tableView = UITableView(frame:UIScreen.main.applicationFrame,style:.grouped)
self.tableView!.delegate = self
self.tableView!.dataSource = self
//创建一个重用的单元格
self.tableView!.register(UITableViewCell.self, forCellReuseIdentifier: "SwiftCell")
self.view.addSubview(self.tableView!)
}
// 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(nameString: aString, pinyinString: pinyinString).uppercased()
// 截取大写首字母
let firstString = strPinYin.substring(to: strPinYin.index(strPinYin.startIndex, offsetBy:1))
// 判断姓名首位是否为大写字母
let regexA = "^[A-Z]$"
let predA = NSPredicate.init(format: "SELF MATCHES %@", regexA)
return predA.evaluate(with: firstString) ? firstString : "#"
}
/// 多音字处理
func polyphoneStringHandle(nameString:String, pinyinString:String) -> String {
if nameString.hasPrefix("长") {return "chang"}
if nameString.hasPrefix("沈") {return "shen"}
if nameString.hasPrefix("厦") {return "xia"}
if nameString.hasPrefix("地") {return "di"}
if nameString.hasPrefix("重") {return "chong"}
return pinyinString;
}
func numberOfSections(in tableView: UITableView) -> Int {
return adHeaders.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let key = adHeaders[section]
let array = addressBookDict[key]
return (array?.count)!
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return adHeaders[section]
}
// 右侧索引
func sectionIndexTitles(for tableView: UITableView) -> [String]? {
return adHeaders
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
if cell == nil {
cell = UITableViewCell.init(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
}
let modelArray = addressBookDict[adHeaders[indexPath.section]]
let model = modelArray![indexPath.row]
cell?.textLabel?.text = model.name
return cell!
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let modelArray = addressBookDict[adHeaders[indexPath.section]]
let model = modelArray![indexPath.row]
let alertViewVC = UIAlertController.init(title: model.name, message:"", preferredStyle:UIAlertControllerStyle.alert)//message:"\(model.mobileArray)"
let confirm = UIAlertAction.init(title: "确定", style: UIAlertActionStyle.cancel, handler:nil)
alertViewVC.addAction(confirm)
self.present(alertViewVC, animated: true, completion: nil)
}
func loadDate_table() {
let parameters: Parameters = [
"action": "getUser",
"account":ServerManager.account
]
let url = ServerManager.getURL2(parameters: parameters)
print(url)
Alamofire.request(url).validate().responseJSON { (response) in
//print(response)
switch response.result {
case .success(let value):
let json = JSON(value)
// print("JSON: \(json)")
let count = json.count
//print("count",count)
if count > 0 {
for i in 0..
self.nameArray.append(json["account"].string!)//
print("nameArray",self.nameArray)
}
}
case .failure(let error):
AlertUtil.alert(message: error as? String ?? "请求超时,连接不上服务器!" )
}
}
}
}
网友评论