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

UITableView实现索引功能

像iOS中的通讯录,通过点击联系人表格右侧的字母索引,我们可以快速定位到以该字母为首字母的联系人分组。
 
要实现索引,我们只需要两步操作:
  • (1)实现索引数据源代理方法
  • (2)响应点击索引触发的代理事件
 
效果图如下:
iOS开发——UI_swift篇&UITableView实现索引功能
 
代码如下:
 1 import UIKit

 2  

 3 class ViewController: UIViewController , UITableViewDelegate, UITableViewDataSource{

 4      

 5     var tableView:UITableView?

 6      

 7     var adHeaders:[String] = ["a","b","c","d","e"]

 8      

 9     override func loadView() {

10         super.loadView()

11     }

12      

13     override func viewDidLoad() {

14         super.viewDidLoad()

15          

16         //创建表视图

17         self.tableView = UITableView(frame:UIScreen.mainScreen().applicationFrame,

18             style:UITableViewStyle.Grouped)

19         self.tableView!.delegate = self

20         self.tableView!.dataSource = self

21         //创建一个重用的单元格

22         self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "SwiftCell")

23         self.view.addSubview(self.tableView!)

24     }

25      

26     //实现索引数据源代理方法

27     func sectionIndexTitlesForTableView(tableView: UITableView) -> [AnyObject]! {

28         return adHeaders

29     }

30      

31     //点击索引,移动TableView的组位置

32     func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String,

33         atIndex index: Int) -> Int {

34         var tpIndex:Int = 0

35         //遍历索引值

36         for character in adHeaders{

37             //判断索引值和组名称相等,返回组坐标

38             if character == title{

39                 return tpIndex

40             }

41             tpIndex++

42         }

43         return 0

44     }

45      

46     //设置分区数

47     func numberOfSectionsInTableView(tableView: UITableView!) -> Int {

48         return adHeaders.count;

49     }

50      

51     //返回表格行数

52     func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

53         return 5

54     }

55      

56     // UITableViewDataSource协议中的方法,该方法的返回值决定指定分区的头部

57     func tableView(tableView:UITableView, titleForHeaderInSection

58         section:Int)->String

59     {

60         var headers =  self.adHeaders;

61         return headers[section];

62     }

63      

64     //设置分组尾部高度(不需要尾部,设0.0好像无效)

65     func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {

66         return 0.1

67     }

68      

69     //创建各单元显示内容(创建参数indexPath指定的单元)

70     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)

71         -> UITableViewCell

72     {

73         //为了提供表格显示性能,已创建完成的单元需重复使用

74         let identify:String = "SwiftCell"

75         //同一形式的单元格重复使用,在声明时已注册

76         let cell = tableView.dequeueReusableCellWithIdentifier(identify, forIndexPath: indexPath)

77             as UITableViewCell

78         cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator

79         var secno = indexPath.section

80         cell.textLabel?.text = self.adHeaders[secno]+String(indexPath.item)

81         return cell

82     }

83      

84     override func didReceiveMemoryWarning() {

85         super.didReceiveMemoryWarning()

86     }

87 }

 

 原文: http://www.hangge.com

你可能感兴趣的:(UITableView)