美文网首页
IOS城市列表添加索引

IOS城市列表添加索引

作者: 向日葵的夏天_summer | 来源:发表于2017-11-07 16:21 被阅读0次
  1. 根据后端返回的城市列表,遍历模型,按照索引(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("#")
     }
    
  2. 实现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
}
  1. 根据字符串获取首字母的方法实现:

    // 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;
    }
    
  2. 总结,需要实现的方法:

    • 返回索引列表的值

       func sectionIndexTitles(for tableView: UITableView) -> [String]? {}
      
    • 组标题跟索引列表的值对应起来

      func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
      
    • 设置索引字体颜色,背景颜色,选中背景颜色

      tableView.sectionIndexColor = UIColor.black  //索引字体颜色
      tableView.sectionIndexBackgroundColor = UIColor.clear  //背景颜色
      tableView.sectionIndexTrackingBackgroundColor   //选中背景颜色

相关文章

  • IOS城市列表添加索引

    根据后端返回的城市列表,遍历模型,按照索引(A--Z)添加到字典中去: //MARK:- 按照城市字母顺序排序fu...

  • 超简单实现iOS列表的索引功能

    超简单实现iOS列表的索引功能 超简单实现iOS列表的索引功能

  • python--列表

    insert 在指定索引位置添加数据 append 在列表末尾添加数据 del 列表[索引] 删除指定索引的数据 ...

  • Python List

    取出元素变量=列表[索引] 添加元素列表.insert(位置,要添加的元素)列表.append(要添加的元素)列表...

  • 列表基础语法

    末尾添加数据 列表.append(数据) 索引处插入数据 列表.insert(索引,数据) 追加一个列表 列...

  • 数据库 | MySQL | 9. 索引

    PRIMARY KEY是唯一性索引 添加表主键 删除&修改表主键 查看索引列表 添加普通索引 添加唯一性索引(设置...

  • Python学习——列表简介

    Python学习——列表操作 列表: 列表内的元素可以通过索引来操作和使用 列表的修改、添加、删除: 修改: 添加...

  • 怎么样才能自定义搜索引擎,比如说添加、修改、排序?

    点击左上角的搜索图标可弹出搜索引擎列表,此列表可以自由定制。 一、添加搜索引擎。 点击列表最底部的加号可以添加新的...

  • Android 用RecylerView实现列表索引实践

    android中经常用到列表索引(如联系人列表、城市列表等) 这种方式在索引列表复杂的情况下(如索引间增加间隔、索...

  • CollectionView添加索引

    在collectionView列表添加索引条, 因不像tableView的协议自带设置索引功能, 所以collec...

网友评论

      本文标题:IOS城市列表添加索引

      本文链接:https://www.haomeiwen.com/subject/ugejmxtx.html