★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-nzvznsoh-kc.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
目錄:[Swift]通天遁地Swiftgit
本文將演示如何給表格添加索引功能。github
在項目導航區,打開視圖控制器的代碼文件【ViewController.swift】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 }