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: "咯理"))
        
        
        
        //獲得當前UILocalizedIndexedCollation對象并且引用賦給collation,A-Z的數(shù)據(jù)
        collation = UILocalizedIndexedCollation.current()
        //獲得索引數(shù)和section標題數(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è)置索引標題
    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)系作者
  • 序言:七十年代末熊响,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子诗赌,更是在濱河造成了極大的恐慌汗茄,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,843評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件铭若,死亡現(xiàn)場離奇詭異洪碳,居然都是意外死亡递览,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,538評論 3 392
  • 文/潘曉璐 我一進店門瞳腌,熙熙樓的掌柜王于貴愁眉苦臉地迎上來绞铃,“玉大人,你說我怎么就攤上這事嫂侍《酰” “怎么了?”我有些...
    開封第一講書人閱讀 163,187評論 0 353
  • 文/不壞的土叔 我叫張陵吵冒,是天一觀的道長。 經(jīng)常有香客問我西剥,道長痹栖,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,264評論 1 292
  • 正文 為了忘掉前任瞭空,我火速辦了婚禮揪阿,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘咆畏。我一直安慰自己南捂,他們只是感情好,可當我...
    茶點故事閱讀 67,289評論 6 390
  • 文/花漫 我一把揭開白布旧找。 她就那樣靜靜地躺著溺健,像睡著了一般。 火紅的嫁衣襯著肌膚如雪钮蛛。 梳的紋絲不亂的頭發(fā)上鞭缭,一...
    開封第一講書人閱讀 51,231評論 1 299
  • 那天,我揣著相機與錄音魏颓,去河邊找鬼岭辣。 笑死,一個胖子當著我的面吹牛甸饱,可吹牛的內(nèi)容都是我干的沦童。 我是一名探鬼主播,決...
    沈念sama閱讀 40,116評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼叹话,長吁一口氣:“原來是場噩夢啊……” “哼偷遗!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起驼壶,我...
    開封第一講書人閱讀 38,945評論 0 275
  • 序言:老撾萬榮一對情侶失蹤鹦肿,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后辅柴,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體箩溃,經(jīng)...
    沈念sama閱讀 45,367評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡瞭吃,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,581評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了涣旨。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片歪架。...
    茶點故事閱讀 39,754評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖霹陡,靈堂內(nèi)的尸體忽然破棺而出和蚪,到底是詐尸還是另有隱情,我是刑警寧澤烹棉,帶...
    沈念sama閱讀 35,458評論 5 344
  • 正文 年R本政府宣布攒霹,位于F島的核電站,受9級特大地震影響浆洗,放射性物質(zhì)發(fā)生泄漏催束。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,068評論 3 327
  • 文/蒙蒙 一伏社、第九天 我趴在偏房一處隱蔽的房頂上張望抠刺。 院中可真熱鬧,春花似錦摘昌、人聲如沸速妖。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,692評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽罕容。三九已至,卻和暖如春稿饰,著一層夾襖步出監(jiān)牢的瞬間杀赢,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,842評論 1 269
  • 我被黑心中介騙來泰國打工湘纵, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留脂崔,地道東北人。 一個月前我還...
    沈念sama閱讀 47,797評論 2 369
  • 正文 我出身青樓梧喷,卻偏偏與公主長得像砌左,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子铺敌,可洞房花燭夜當晚...
    茶點故事閱讀 44,654評論 2 354

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,085評論 25 707
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫汇歹、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,096評論 4 62
  • 鐘王顏柳書千古偿凭,醉素癲張龍蛇舞产弹。 空靈飄逸屬趙董,蒼潤奇崛張瑞圖。
    于新源閱讀 240評論 2 7
  • 每一種風格都有他的邏輯痰哨。 無賴的邏輯就是強行滿地打滾胶果,在這個意義上,無賴是不可戰(zhàn)勝的斤斧,除非你打滾打得比他更溜早抠。 所...
    個體發(fā)展研究閱讀 703評論 0 51
  • 獲取更多演出信息,就到現(xiàn)場互娛撬讽,現(xiàn)場互娛蕊连,您身邊的現(xiàn)場娛樂管家,提供全國5000場演出和賽事信息游昼,完美體驗甘苍,你我同...
    現(xiàn)場互娛閱讀 353評論 0 0