IOS城市列表添加索引
2017-11-07 本文已影响0人
向日葵的夏天_summer
-
根据后端返回的城市列表,遍历模型,按照索引(A--Z)添加到字典中去:
//MARK:- 按照城市字母顺序排序 func getDisplayData() { for i in 0..<cityItems.count { let item = cityItems[i] let firstLetter = String.getFirstLetterFromString(aString: item.NAME) if cityDispalyData[firstLetter] != nil { cityDispalyData[firstLetter]?.append(item) }else { cityDispalyData[firstLetter] = [item] cityIndexTitles.append(firstLetter) } } //索引标题数组,按照A--Z排序 cityIndexTitles = cityIndexTitles.sorted(by: <) cityIndexTitles.append("#") }
-
实现tableView的代理和数据源方法
func numberOfSections(in tableView: UITableView) -> Int {
return cityDispalyData.count + 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0:
return 1
default:
return cityDispalyData[cityIndexTitles[section - 1]]?.count ?? 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: ChangeCityCell = tableView.dequeueReusableCell(for: indexPath)
return cell
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let cell = cell as? ChangeCityCell
switch indexPath.section {
case 0:
let cityDict = userManager.object(forKey: MainCity_Key) as? NSMutableDictionary
let cityItem = CityListModel(dict: cityDict as! [String: AnyObject])
cell?.reloadWithModel(model: cityItem)
default:
if cityDispalyData.count>0 {
if let array = cityDispalyData[cityIndexTitles[indexPath.section - 1]] {
let cityItem = array[indexPath.row]
cell?.reloadWithModel(model: cityItem)
}
}
}
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
switch section {
case 0:
let locationTitle = UILabel()
locationTitle.text = " 当前选择城市"
locationTitle.font = UIFont(name: Font_Thin, size: 14)
return locationTitle
default:
let locationTitle = UILabel()
locationTitle.text = " \(cityIndexTitles[section - 1])"
locationTitle.font = UIFont(name: Font_Thin, size: 14)
return locationTitle
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return cellHeight
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0.0001
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 40
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
switch indexPath.section {
case 0:
NotificationManager.post(name: backCityArea_NoticeKey, object: nil)
default:
let array = cityDispalyData[cityIndexTitles[indexPath.section - 1]]
guard let cityItem = array?[indexPath.row] else {return}
saveCityItem(model: cityItem)
if let block = changedCityBlock {
block(cityItem)
}
}
}
func sectionIndexTitles(for tableView: UITableView) -> [String]? {
return cityIndexTitles
}
func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
return index
}
-
根据字符串获取首字母的方法实现:
// MARK: - 获取首字母(传入汉字字符串, 返回大写拼音首字母) static 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.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 : "#"
}
/// 多音字处理
static 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; }
-
总结,需要实现的方法:
-
返回索引列表的值
func sectionIndexTitles(for tableView: UITableView) -> [String]? {}
-
组标题跟索引列表的值对应起来
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
-
设置索引字体颜色,背景颜色,选中背景颜色
tableView.sectionIndexColor = UIColor.black //索引字体颜色 tableView.sectionIndexBackgroundColor = UIColor.clear //背景颜色 tableView.sectionIndexTrackingBackgroundColor //选中背景颜色
-