[Swift通天遁地]二、表格表单-(2)创建右侧带有索引的UITableView(表单视图)
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10181397.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
本文将演示如何给表格添加索引功能。
在项目导航区,打开视图控制器的代码文件【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 }