美文网首页ios⋯⋯runloop runtimeiOS随笔-by玛丽
Swift城市列表---按字母排序分组

Swift城市列表---按字母排序分组

作者: MMD_ | 来源:发表于2018-03-22 12:58 被阅读125次

    话不多说直接添代码

    //
    //  MLSelectCityViewController.swift
    //  zhongXiang_Swift
    //
    //  Created by 玛丽 on 2018/3/12.
    //  Copyright © 2018年 玛丽. All rights reserved.
    //
    
    import UIKit
    import ObjectMapper
    import RealmSwift
    
    class MLSelectCityViewController: MLBaseViewController {
        
        func requestData() {
            
            MLHttpTool.getAllCityList(parameters: nil) { (response) in
                
                let modelArr = Mapper<MLCityModel>().mapArray(JSONObject: response)
                guard modelArr?.count != 0 else {
                    return
                }
                
                for model in modelArr! {
                    model.letter = self.getFirCharactor(str: model.name!)
                }
                
                self.dataArray.setArray(modelArr!)
                
                //首字母升序排列
                self.getAllCityMetter()
                
                self.tableView.reloadData()
            }
        }
        
        //获得所有的key值并排序,并返回排好序的数组
        func getAllCityKey()->NSArray{
            let keys = metterDict.allKeys as NSArray
            return keys.sortedArray(using: #selector(NSNumber.compare(_:))) as NSArray
        }
        
        //首字母相同的放在一起
        func getAllCityMetter(){
            for cityInfo in self.dataArray {
                let info = cityInfo as! MLCityModel
                let s = metterDict[info.letter ?? ""]
                if s == nil {
                    var letterArr: NSMutableArray
                    letterArr = NSMutableArray()
                    metterDict.setObject(letterArr, forKey: info.letter! as NSCopying)
                    letterArr.add(info)
                }else{
                    let m_letterArr = metterDict[info.letter ?? ""] as! NSMutableArray
                    m_letterArr.add(info)
                }
            }
        }
        
        func getFirCharactor(str: String) -> String {
            
            let mString = NSMutableString(string: str)
            CFStringTransform(mString, nil, kCFStringTransformToLatin, false)
            CFStringTransform(mString, nil, kCFStringTransformStripDiacritics, false)
            let string = mString.capitalized
            return string.substring(to: string.index(string.startIndex, offsetBy: 1))
        }
        
    }
    
    extension MLSelectCityViewController: UITableViewDataSource, UITableViewDelegate
    {
        func numberOfSections(in tableView: UITableView) -> Int {
            let keys = self.getAllCityKey()
            return keys.count
        }
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            let keys = self.getAllCityKey()
            let keyStr = keys[section]
            let values = metterDict.object(forKey: keyStr)
            return ((values as AnyObject).count)!
        }
        
        //索引代理
        func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
            let keys = self.getAllCityKey() as! [String]
            return keys
        }
        func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
            let keys = self.getAllCityKey() as! [String]
            //判断keys是否为空
            if keys.count > 0 {
                return keys[section]
            }else{
                return nil
            }
            
        }
        
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
            let keys = self.getAllCityKey()//所有的key值
            let keyStr = keys[indexPath.section]//选择的是哪一组
            let values = metterDict.object(forKey: keyStr) as! NSArray//获取该组下面的所有行
            let info = values[indexPath.row] as! MLCityModel//获取选中的行
            
            cell.textLabel?.text = info.name
            cell.textLabel?.font = UIFont.systemFont(ofSize: 14)
            
            return cell
        }
    }
    
    

    相关文章

      网友评论

        本文标题:Swift城市列表---按字母排序分组

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