標籤:ios swift uitableview 索引 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 UIKitclass 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-IndexList4、結語
參考資料如下:
About Table Views in iOS Apps
文章最後更新時間:2015年4月14日09:02:50
【精】表格(UITableView)總結(2):索引(IndexList)