效果圖
有個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
}
}