1. 程式人生 > >[Swift通天遁地]二、表格表單-(2)建立右側帶有索引的UITableView(表單檢視)

[Swift通天遁地]二、表格表單-(2)建立右側帶有索引的UITableView(表單檢視)

本文將演示如何給表格新增索引功能。

在專案導航區,開啟檢視控制器的程式碼檔案【ViewController.swift】

現在開始編寫程式碼,建立一個表格,並在表格右側新增一列快捷索引。

  1 import UIKit
  2 
  3 //使當前的檢視控制器類,遵循表格的資料來源協議UITableViewDataSource
  4 class ViewController: UIViewController, UITableViewDataSource {
  5 
  6     //建立一個字典物件,作為表格的資料來源。字典中的鍵,將作為表格的索引列表。
  7     var
countries :Dictionary<String, [String]> = ["A": ["Afghanistan", "Albania", "Algeria", "Angola", "Australia", "Austria", "Azerbaijan"], 8 "B":["Bangladesh","Belgium","Bhutan","Bolivia","Brazil","Bahrain","Bulgaria"], 9 "C":["Canada","Congo","Chile","China","Colombia","Cuba"],
10 "D":["Denmark","Djibouti","Dominica"], 11 "E":["Egypt","Estonia","Ethiopia"], 12 "F":["Fiji","Finland","France"], 13 "G":["Gambia","Germany","Greece"], 14 "H":["Haiti","Honduras","Hungary"], 15 "I":["India","Indonesia","Iran","Ireland","Iraq","Italy"], 16 "
J":["Jordan","Japan"], 17 "K":["Kazakhstan","Korea","Kuwait"], 18 "L":["Laos","Libya","Lebanon"], 19 "M":["Madagascar","Morocco","Malaysia","Mexico","Mali","Mozambique"], 20 "N":["Nepal","Netherlands","Nigeria","New Zealand"], 21 "O":["Oman"], 22 "P":["Pakistan","Panama","Philippines","Portugal"], 23 "Q":["Qatar"], 24 "R":["Romania","South Africa","Russia"], 25 "S":["Serbia & Montenegro","Senegal","Singapore","Somalia","Switzerland"], 26 "T":["Thailand","Turkmenistan","Tunisia","Turkey"], 27 "U":["United Arab Emirates","United States of America","Uzbekistan"], 28 "V":["Vanuatu","Venezuela","Vietnam"], 29 "Y":["Yemen"], 30 "Z":["Zambia","Zimbabwe"]] 31 32 //建立一個字串陣列,作為當前類的另一個屬性 33 var keys:[String] = [] 34 35 override func viewDidLoad() { 36 super.viewDidLoad() 37 // Do any additional setup after loading the view, typically from a nib. 38 39 //將字典的鍵轉換為陣列,並執行升序排列,這個陣列將被作為索引使用 40 keys = Array(countries.keys).sorted() 41 42 //獲得裝置的螢幕尺寸 43 let screenRect = UIScreen.main.bounds 44 //建立一個矩形區域,作為表格檢視的顯示區域。 45 let tableRect = CGRect(x: 0, 46 y: 20, 47 width: screenRect.size.width, 48 height: screenRect.size.height - 20) 49 //初始化一個指定顯示區域的表格物件 50 let tableView = UITableView(frame: tableRect) 51 52 //設定表格物件的資料來源為當前的檢視控制器物件 53 tableView.dataSource = self 54 //並將表格檢視新增到根檢視中 55 self.view.addSubview(tableView) 56 } 57 58 //新增一個代理方法,用來設定表格的段落的數量 59 func numberOfSections(in tableView: UITableView) -> Int 60 { 61 //在此設定段落的數量,等於字典中鍵的數量 62 return keys.count 63 } 64 65 //新增一個代理方法,用來設定表格的行數 66 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 67 { 68 //獲得當前段落的序號, 69 let subCountries = countries[keys[section]] 70 //然後獲得在字典中,對應的值的數量 71 //以該數量作為當前段落的行數 72 return (subCountries?.count)! 73 } 74 75 //新增一個代理方法,用來設定表格的段落標題 76 func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? 77 { 78 return keys[section] 79 } 80 81 //新增一個代理方法,用來設定表格索引的標題陣列 82 func sectionIndexTitles(for tableView: UITableView) -> [String]? 83 { 84 return keys 85 } 86 87 //新增一個代理方法,用來初始化或複用表格中的單元格 88 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 89 { 90 //建立一個字串常量,作為單元格的複用標識 91 let identifier = "reusedCell" 92 //根據複用標識,從表格中獲得可以複用的單元格 93 var cell = tableView.dequeueReusableCell(withIdentifier: identifier) 94 95 //如果沒有可以複用的單元格 96 if(cell == nil) 97 { 98 //則初始化一個預設樣式的單元格,並設定單元格的複用標識 99 cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: identifier) 100 } 101 102 //根據當前單元格的段落序號,獲得國家名稱列表 103 let subCountries = countries[keys[(indexPath as NSIndexPath).section]] 104 //根據當前單元格的序號,獲得該單元格需要顯示的國家名稱 105 cell?.textLabel?.text = subCountries![(indexPath as NSIndexPath).row] 106 107 //返回設定好的單元格 108 return cell! 109 } 110 111 override func didReceiveMemoryWarning() { 112 super.didReceiveMemoryWarning() 113 // Dispose of any resources that can be recreated. 114 } 115 }