【精】表格(UITableView)总结(2):索引(IndexList)

转载请声明出处:http://blog.csdn.net/jinnchang/article/details/45037159

1、前言

当我们的 Table View 中数据量比较大、分段比较多的时候我们可以引入 IndexList 来方便地完成分段的定位,例如系统的通讯录程序。我们可以通过设置 Table View 的 sectionIndexMinimumDisplayRowCount 属性来指定当 Table View 中有多少行的时候开始显示 IndexList,默认的设置是 NSIntegerMax,即默认是不显示 IndexList
的。

为了能够使用 IndexList 我们还需要实现 UITableViewDataSource 中以下两个方法:

// 设置分段索引数组
func sectionIndexTitlesForTableView(tableView: UITableView) -> [AnyObject]!

// 点击索引触发事件(一般不需要写,默认 section index 顺序与 section 对应)
func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int

2、演示示例

3、演示代码

//
//  ViewController.swift
//  UITableViewSample-IndexList
//
//  Created by jinnchang on 15/4/13.
//  Copyright (c) 2015年 Jinn Chang. All rights reserved.
//

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var tableView: UITableView!
    var data = ["C" : ["蔡依林", "蔡健雅"], "F" : ["范玮琪", "范晓萱", "方大同", "费玉清", "费翔", "付笛生"],
        "L" : ["梁静茹", "林忆莲", "李健", "林俊杰", "林宥嘉"], "T" : ["田馥甄", "谭维维"],
        "Z" : ["张靓颖", "张惠妹", "卓依婷", "周杰伦", "张杰", "张学友", "张国荣", "张信哲", "周传雄", "周华健", "张震岳", "张敬轩", "张宇"]]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        tableView = UITableView(frame: CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height), style: .Plain)
        tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
        tableView.delegate = self
        tableView.dataSource = self
        // 设置索引颜色
        tableView.sectionIndexColor = UIColor.redColor()
        // 设置索引背景颜色
        tableView.sectionIndexBackgroundColor = UIColor.clearColor()

        self.view.addSubview(tableView)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // 设置分段数
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return data.count
    }

    // 设置每个分段标题(即索引内容)
    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return data.keys.array[section]
    }

    // 设置分段索引数组
    func sectionIndexTitlesForTableView(tableView: UITableView) -> [AnyObject]! {
        return data.keys.array
    }

    // 点击索引触发事件(一般不需要写,默认 section index 顺序与 section 对应)
    func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int {
        let selectedIndexPath = NSIndexPath(forRow: 0, inSection: index)
        tableView.scrollToRowAtIndexPath(selectedIndexPath, atScrollPosition: .Bottom, animated: true)
        return index
    }

    // 设置每个分段对应的行数
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data[data.keys.array[section]]!.count
    }

    // 设置每行的具体内容
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
        let sectionData = data[data.keys.array[indexPath.section]]
        cell.textLabel?.text = sectionData![indexPath.row]
        return cell
    }

}

Github上项目地址:https://github.com/jinnchang/SwiftSamples/tree/master/advanced/UITableViewSample-IndexList

4、结语

参考资料如下:

About
Table Views in iOS Apps

文章最后更新时间:2015年4月14日09:02:50

时间: 2024-08-03 16:14:43

【精】表格(UITableView)总结(2):索引(IndexList)的相关文章

Swift - 给表格UITableView添加索引功能(快速定位)

像iOS中的通讯录,通过点击联系人表格右侧的字母索引,我们可以快速定位到以该字母为首字母的联系人分组. 要实现索引,我们只需要两步操作: (1)实现索引数据源代理方法 (2)响应点击索引触发的代理事件 效果图如下: 代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 4

iOS中表格(UITableView)嵌套表格的简单实现

iOS中表格(UITableView)嵌套表格的简单实现 首先说一下思路:我们在一个控制器里面定义2个tableview,一个作为被嵌套的rootTable,一个作为嵌套的表格tableView1,那我们要实现UITableViewDelegate,UITableViewDataSource,的代理的时候,该怎么区分呢?其实很简单,有两种方法,一个是给定义的2个tableview设置tag值,另一个是直接写出来tableView == rootTable时实现他得代理,否则就实现tableVie

swift:创建表格UITableView

用swift创建单元格和用iOS创建单元格形式基本相同,就是语法上有些异样.swift中调用成员方法不再使用[ ]来发送消息,而是使用.成员方法的形式调用成员函数.这种格式非常类似于java中的点成员运算符.swift中对其他类的引用不用导入头文件.这里就不废话了,现在纯代码创建UITableview实例如下: 具体实例如下: 1.首先用swift创建一个工程Project 2.再用swift创建一个Person类,生成Person.swift文件 3.在Perosn.swift文件中将设置的属

2018.12.7边界圆角redius,背景图设置,平铺,精灵图,盒子伪类索引

一选择器复习 <!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>复习预习</title></head><body> 复习预习 <!-- 1.组合选择器 --> <!-- 群组选择器: div, #div, .div { 该样式块同时控制多个(div, #div, .div) } 每一个选择器位均可以为基础选择

IOS UITableView分组与索引分区实例

1 #import <UIKit/UIKit.h> 2 3 @interface AppDelegate : UIResponder <UIApplicationDelegate> 4 5 @property (strong, nonatomic) UIWindow *window; 6 7 8 @end 1 #import "AppDelegate.h" 2 #import "RootViewController.h" 3 @interfa

iOS UITableView表格索引

 if ([myTableView respondsToSelector:@selector(setSectionIndexColor:)]) {         myTableView.sectionIndexColor = [UIColor redColor];         myTableView.sectionIndexBackgroundColor = [UIColor yellowColor];     } for(UIView *view in [tableView subvie

iOS开发——UI_swift篇&amp;UITableView实现索引功能

UITableView实现索引功能 像iOS中的通讯录,通过点击联系人表格右侧的字母索引,我们可以快速定位到以该字母为首字母的联系人分组. 要实现索引,我们只需要两步操作: (1)实现索引数据源代理方法 (2)响应点击索引触发的代理事件 效果图如下: 代码如下: 1 import UIKit 2 3 class ViewController: UIViewController , UITableViewDelegate, UITableViewDataSource{ 4 5 var tableV

简述UITableView的属性和用法

UITableView UITableView内置了两种样式:UITableViewStylePlain,UITableViewStyleGrouped   <UITableViewDataSource,UITableViewDelegate>里的方法: tableView处理步骤 1.有多少组 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 2.第section组头部控件有多高 - (CGFloat)tabl

黑马程序员——UITableView的详细用法

------<a href="http://www.itheima.com" target="blank">Java培训.Android培训.iOS培训..Net培训</a>.期待与您交流! ------- UITableView内置了两种样式:UITableViewStylePlain,UITableViewStyleGrouped <UITableViewDataSource,UITableViewDelegate>里的方法:

(转载)UITableView的详细讲解

NSIndexPath类型是用来获取用户选择的indexPath,在别的函数里面,若需要知道用户选择了哪个cell,用上它可以省事很多.不必再去建全局变量section和row. NSIndexPath *tableSelection = [self.tableView indexPathForSelectedRow]; 1.    UITableView的初始化 UITableView tableview= [[UITableView alloc] initWithFrame:CGRectMa