拼音索引java_swift3.0 实现中文拼音索引

效果图

94da66f077eb?nomobile=yes

QQ20160907-0@2x.png

有个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..

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..

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

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值