美文网首页
swift3.0以上版本的仿通信录,实现中文拼音索引完整版(无b

swift3.0以上版本的仿通信录,实现中文拼音索引完整版(无b

作者: iOS_小小简 | 来源:发表于2018-10-10 17:31 被阅读152次

//直接整体全部copy此类,文本编辑没有美观美化操作

//直接集成storyboard继承类即可运行,需要原版的Demo私信

import UIKit

class ViewController: UITableViewController {

var userArray: [Userr] = [Userr]()
var sectionsArray: [[Userr]] = [[Userr]]()

var indexArray: [String] = [String]()

// tableView 索引搜索工具类
var collation: UILocalizedIndexedCollation? = nil

override func viewDidLoad() {
    
    super.viewDidLoad()
    tableView.backgroundColor = UIColor.yellow
    tableView.delegate = self
    tableView.dataSource = self
    self.configureSection()
    
}

func configureSection() {
    
    //自定义的模拟数据
    for i in 0...3 {
        userArray.append(Userr(name: "合理\(i)"))
        userArray.append(Userr(name: "a合理\(i)"))
        userArray.append(Userr(name: "你合理\(i)"))
        userArray.append(Userr(name: "咯合理\(i)"))
        userArray.append(Userr(name: "g合理\(i)"))
    }
    userArray.append(Userr(name: "咯理"))
    
    //获得当前UILocalizedIndexedCollation对象并且引用赋给collation,A-Z的数据
    collation = UILocalizedIndexedCollation.current()
    //获得索引数和section标题数
    let sectionTitlesCount = collation!.sectionTitles.count
    
    //临时数据,存放section对应的userObjs数组数据
    var newSectionsArray = [[Userr]]()
    
    //设置sections数组初始化:元素包含userObjs数据的空数据
    for _ in 0..<sectionTitlesCount {
        let array = [Userr]()
        newSectionsArray.append(array)
    }
    
    //将用户数据进行分类,存储到对应的sesion数组中
    for bean in userArray {
        //根据timezone的localename,获得对应的的section number
        let sectionNumber = collation?.section(for: bean, collationStringSelector: #selector( getter: Userr.name))
        
        //获得section的数组
        var sectionBeans = newSectionsArray[sectionNumber!]
        
        //添加内容到section中
        sectionBeans.append(bean)
        
        // swift 数组是值类型,要重新赋值
        newSectionsArray[sectionNumber!] = sectionBeans
    }
    
    
    //排序,对每个已经分类的数组中的数据进行排序,如果仅仅只是分类的话可以不用这步
    for i in 0..<sectionTitlesCount {
        
        let beansArrayForSection = newSectionsArray[i]
        //获得排序结果
        let sortedBeansArrayForSection = collation?.sortedArray(from: beansArrayForSection, collationStringSelector:  #selector(getter: Userr.name))
        //替换原来数组
        newSectionsArray[i] = sortedBeansArrayForSection as! [Userr]
    }
    
    //数组要去空操作,优化解决bug问题
    var temp2 = [[Userr]]()
    for indexxxxx in 0..<newSectionsArray.count {
        
        let temp = newSectionsArray[indexxxxx]

        if !temp.isEmpty{
            
            if let headserString = collation?.sectionTitles[indexxxxx] {
                if !indexArray.contains(headserString) {
                    indexArray.append(headserString)
                }
            }
            temp2.append(temp)
        }
    }
    sectionsArray = temp2
}

}

// MARK: - tableviewDelegate
extension ViewController{

override func numberOfSections(in tableView: UITableView) -> Int {
    
    return sectionsArray.count
    
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
    let beanInSection = sectionsArray[section]
    return beanInSection.count
}

//设置每行的cell的内容
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let id = "id"
    var cell = tableView.dequeueReusableCell(withIdentifier: id)
    if cell == nil {
        cell = UITableViewCell(style: .default, reuseIdentifier: id)
    }
    let userNameInSection = sectionsArray[indexPath.section]
    let bean = userNameInSection[indexPath.row]
    cell?.textLabel?.text = (bean as AnyObject).name
    return cell!
}

/*
 * 跟section有关的设定
 */
//设置section的Header
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    
    let temp = self.indexArray[section]
    return temp
    
}

//设置索引标题
override func sectionIndexTitles(for tableView: UITableView) -> [String]? {
    
    // return collation?.sectionTitles
    return indexArray
}

//关联搜索
override func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
    //原始的代码有bug代码
    //return (collation?.section(forSectionIndexTitle: index))!
    //
    tableView.scrollToRow(at: IndexPath.init(row: 0, section: index) , at: .top, animated: true)
    return index
}

}

class Userr: NSObject{
@objc var name: String?
init(name: String){
self.name = name
}
}

相关文章

网友评论

      本文标题:swift3.0以上版本的仿通信录,实现中文拼音索引完整版(无b

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