swift3.0 實現(xiàn)中文拼音索引

效果圖


QQ20160907-0@2x.png

有個bug: 索引與title跳動沒有符合實際,現(xiàn)在沒有解決思路,后續(xù)解決

代碼注釋比較清楚,一個文件完成功能

//
//  ViewController.swift
//  TableViewIndex
//
//  Created by mba on 16/9/7.
//  Copyright ? 2016年 mbalib. All rights reserved.
//

import UIKit

class ViewController: UITableViewController {

    var userArray: [Userr] = [Userr]()
    var sectionsArray: [[Userr]] = [[Userr]]()
    
    
    var indexArray: [String] = [String]()
    
    // uitableView 索引搜索工具類
    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: "咯理"))
        
        
        
        //獲得當(dāng)前UILocalizedIndexedCollation對象并且引用賦給collation,A-Z的數(shù)據(jù)
        collation = UILocalizedIndexedCollation.current()
        //獲得索引數(shù)和section標(biāo)題數(shù)
        let sectionTitlesCount = collation!.sectionTitles.count

        //臨時數(shù)據(jù),存放section對應(yīng)的userObjs數(shù)組數(shù)據(jù)
        var newSectionsArray = [[Userr]]()

        //設(shè)置sections數(shù)組初始化:元素包含userObjs數(shù)據(jù)的空數(shù)據(jù)
        for _ in 0..<sectionTitlesCount {
            let array = [Userr]()
            newSectionsArray.append(array)
        }
        
        //將用戶數(shù)據(jù)進行分類,存儲到對應(yīng)的sesion數(shù)組中
        
        for bean in userArray {
            //根據(jù)timezone的localename,獲得對應(yīng)的的section number
            let sectionNumber = collation?.section(for: bean, collationStringSelector: #selector( getter: Userr.name))
            //獲得section的數(shù)組
            var sectionBeans = newSectionsArray[sectionNumber!]
            
            //添加內(nèi)容到section中
            sectionBeans.append(bean)
            
            // swift 數(shù)組是值類型,要重新賦值
            newSectionsArray[sectionNumber!] = sectionBeans
        }
        
        
         //排序,對每個已經(jīng)分類的數(shù)組中的數(shù)據(jù)進行排序,如果僅僅只是分類的話可以不用這步
        for i in 0..<sectionTitlesCount {
            let beansArrayForSection = newSectionsArray[i]
             //獲得排序結(jié)果
            let sortedBeansArrayForSection = collation?.sortedArray(from: beansArrayForSection, collationStringSelector:  #selector(getter: Userr.name))
            //替換原來數(shù)組
            newSectionsArray[i] = sortedBeansArrayForSection as! [Userr]
            
        }

        sectionsArray = newSectionsArray
    }
}

// MARK: - tableviewDelegate
extension ViewController{
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return (collation?.sectionTitles.count)!
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let beanInSection = sectionsArray[section]
        return beanInSection.count
    }
    
    //設(shè)置每行的cell的內(nèi)容
    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有關(guān)的設(shè)定
     */
    //設(shè)置section的Header
    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        let beansInSection = sectionsArray[section]
        if (beansInSection as AnyObject).count <= 0 {
            return nil
        }
        if let headserString = collation?.sectionTitles[section] {
            if !indexArray.contains(headserString) {
                indexArray.append(headserString)
            }
            return headserString
        }
        return nil
    }
    
    //設(shè)置索引標(biāo)題
    override func sectionIndexTitles(for tableView: UITableView) -> [String]? {
//        return collation?.sectionTitles
        return indexArray
    }
    
    //關(guān)聯(lián)搜索
    override func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
          return (collation?.section(forSectionIndexTitle: index))!
      }
  }


 class Userr: NSObject{
   var name: String?
    init(name: String){
        self.name = name
    }
}  
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容