效果图
有个bug: 索引与title跳动没有符合实际,现在没有解决思路,后续解决
代码注释比较清楚,一个文件完成功能
//
// 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的数据
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]
}
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
}
//设置每行的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 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
}
//设置索引标题
override func sectionIndexTitles(for tableView: UITableView) -> [String]? {
// return collation?.sectionTitles
return indexArray
}
//关联搜索
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
}
}