iOS開發——UI_swift篇&UITableView實現索引功能

來源:互聯網
上載者:User

標籤:

UITableView實現索引功能 像iOS中的通訊錄,通過點擊連絡人表格右側的字母索引,我們可以快速定位到以該字母為首字母的連絡人分組。 要實現索引,我們只需要兩步操作:
  • (1)實現索引資料來源代理方法
  • (2)響應點擊索引觸發的代理事件
 如下: 代碼如下:
 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 = self20         self.tableView!.dataSource = self21         //建立一個重用的儲存格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 adHeaders29     }30      31     //點擊索引,移動TableView的組位置32     func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String,33         atIndex index: Int) -> Int {34         var tpIndex:Int = 035         //遍曆索引值36         for character in adHeaders{37             //判斷索引值和組名稱相等,返回組座標38             if character == title{39                 return tpIndex40             }41             tpIndex++42         }43         return 044     }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 554     }55      56     // UITableViewDataSource協議中的方法,該方法的傳回值決定指定分區的頭部57     func tableView(tableView:UITableView, titleForHeaderInSection58         section:Int)->String59     {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.167     }68      69     //建立各單元顯示內容(建立參數indexPath指定的單元)70     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)71         -> UITableViewCell72     {73         //為了提供表格顯示效能,已建立完成的單元需重複使用74         let identify:String = "SwiftCell"75         //同一形式的儲存格重複使用,在聲明時登入76         let cell = tableView.dequeueReusableCellWithIdentifier(identify, forIndexPath: indexPath)77             as UITableViewCell78         cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator79         var secno = indexPath.section80         cell.textLabel?.text = self.adHeaders[secno]+String(indexPath.item)81         return cell82     }83      84     override func didReceiveMemoryWarning() {85         super.didReceiveMemoryWarning()86     }87 }

 

 

iOS開發——UI_swift篇&UITableView實現索引功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.