swift UITableView(八)
2015-11-09 13:58
369 查看
UITableView添加索引
我们上一节主要讲了tableView编辑功能
继续使用上节代码(代码下载方式见第七节末尾)
我们对tableView的索引应该很常见
比如手机联系人右侧的字母索引
如上图所示,我们需要实现一个代理方法
[html] view
plaincopy
//添加右侧索引
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分区的
[html] view
plaincopy
//绑定分区与索引的联系
func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int {
//此处返回index 意思就是索引的序号对应table分区的序号,点击第一个所以table就现实第一个分区
return index
}
这里我们简单设置了一下,将字母顺序和table的分区对应上了。点击索引A跳转到table 分区0 点击索引C跳转到table的第三个分区
我们上一节主要讲了tableView编辑功能
继续使用上节代码(代码下载方式见第七节末尾)
我们对tableView的索引应该很常见
比如手机联系人右侧的字母索引
如上图所示,我们需要实现一个代理方法
[html] view
plaincopy
//添加右侧索引
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分区的
[html] view
plaincopy
//绑定分区与索引的联系
func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int {
//此处返回index 意思就是索引的序号对应table分区的序号,点击第一个所以table就现实第一个分区
return index
}
这里我们简单设置了一下,将字母顺序和table的分区对应上了。点击索引A跳转到table 分区0 点击索引C跳转到table的第三个分区
相关文章推荐
- swift UITableView(七)
- swift UITableView(七)
- swift UITableView(六)
- swift UITableView(六)
- swift UITableView(五)
- swift UITableView(五)
- swift UITableView(四)
- swift UITableView(四)
- swift UITableView(二)
- swift UITableView(二)
- swift UITableView(一)
- swift UITableView(一)
- swift UIScrollView简单介绍
- swift UIScrollView简单介绍
- swift UITabBarController标签栏控制器
- swift UITabBarController标签栏控制器
- swift 设置 navigation左右两侧按钮
- swift 设置 navigation左右两侧按钮
- swift UINavigationController属性设置
- swift UINavigationController属性设置