UITableView添加索引
我们上一节主要讲了tableView编辑功能
继续使用上节代码(代码下载方式见第七节末尾)
我们对tableView的索引应该很常见
比如手机联系人右侧的字母索引
如上图所示,我们需要实现一个代理方法
- //添加右侧索引
- func sectionIndexTitlesForTableView(tableView: UITableView) -> [AnyObject]! {
- var indexes=[String]()
- for i in 0...25
- {
- var ch=String(format: "%c",i+65)
- indexes.append(ch)
- }
- return indexes
- }
看下效果
效果出来了。但是,我们发现当我们点击右侧索引的时候好像和table 的关系不明确。
其实我们少了一个代理方法,他是专门用来关联索引和table分区的
- //绑定分区与索引的联系
- func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int {
- //此处返回index 意思就是索引的序号对应table分区的序号,点击第一个所以table就现实第一个分区
- return index
- }