這一篇詳細介紹表視圖的用法,從組成結構、相關類別到實際範例,一次講清楚。
表視圖是 iOS 開發中最重要的視圖之一,以列表形式展示資料。它由以下幾部分組成:
UITableView 繼承自 UIScrollView,並採用兩個協議:UITableViewDelegate 與 UITableViewDataSource。此外,UITableViewCell 是單元格類別,UITableViewController 是 UITableView 的控制器,UITableViewHeaderFooterView 則用來為節頭與節腳提供視圖。

單元格由圖示、主標題、副標題、擴展視圖組成,可視需要選用。內建的擴展視圖在枚舉類型中定義:
內建的單元格樣式則定義在枚舉類型 UITableViewCellStyle 中:
UITableViewDataSource 主要為表視圖提供資料,主要方法如下:
UITableViewDelegate 主要用來設定節頭、節腳的標題以及處理各種動作事件,主要方法如下:
步驟:
實作如下:
// // ViewController.swift // TableViewDemo // // Created by Michael on 2016/10/26. // Copyright © 2016年 Michael. All rights reserved. // import UIKit class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate { //全部數據 var listItems: NSArray! override func viewDidLoad() { super.viewDidLoad() //讀取資源文件數據 let listPath = Bundle.main.path(forResource: "team", ofType: "plist") self.listItems = NSArray(contentsOfFile: listPath!) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } //返回列表每行的視圖 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { //根據Identifier找到Cell let cell = tableView.dequeueReusableCell(withIdentifier: "CustomId", for: indexPath) let row = indexPath.row let rowDict = self.listItems[row] as! NSDictionary cell.textLabel?.text = rowDict["name"] as? String cell.detailTextLabel?.text = "123" let imagePath = String(format: "%@.png", rowDict["image"] as! String) cell.imageView?.image = UIImage(named: imagePath) cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator return cell } //返回條目數目 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.listItems.count } //響應條目點擊事件 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { print("點擊事件") } }
示例圖,none 模式:

disclosureIndicator 模式:

步驟:
實作如下:
import UIKit class ViewController: UITableViewController { //全部數據 var listItems: NSArray! override func viewDidLoad() { super.viewDidLoad() //讀取資源文件數據 let listPath = Bundle.main.path(forResource: "team", ofType: "plist") self.listItems = NSArray(contentsOfFile: listPath!) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } //返回列表每行的視圖 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "CustomId", for: indexPath) let row = indexPath.row let rowDict = self.listItems[row] as! NSDictionary cell.textLabel?.text = rowDict["name"] as? String cell.detailTextLabel?.text = "123" let imagePath = String(format: "%@.png", rowDict["image"] as! String) cell.imageView?.image = UIImage(named: imagePath) cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator return cell } //返回條目數目 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.listItems.count } //響應條目點擊事件 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { print("點擊事件") } }
示例圖,detailButton 模式:

checkmark 模式:

步驟:
CustomCell 類別:
// // CustomCell.swift // CustomCell // // Created by Michael on 2016/10/25. // Copyright © 2016年 Michael. All rights reserved. // import UIKit class CustomCell: UITableViewCell { @IBOutlet weak var mImage: UIImageView! @IBOutlet weak var mLabel: UILabel! override func awakeFromNib() { super.awakeFromNib() // Initialization code } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state } }
ViewController 類別:
// // ViewController.swift // SimpleTableView // // Created by Michael on 2016/10/24. // Copyright © 2016年 Michael. All rights reserved. // import UIKit class ViewController: UITableViewController { var listItems: NSArray! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let listPath = Bundle.main.path(forResource: "team", ofType: "plist") self.listItems = NSArray(contentsOfFile: listPath!) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.listItems.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { //找到自定義單元格 let cell:CustomCell! = tableView.dequeueReusableCell(withIdentifier: "CustomCellId", for: indexPath) as? CustomCell //let cell = UITableViewCell(style: .value1, reuseIdentifier: "CellIdentifier") let row = indexPath.row let rowDict = self.listItems[row] as! NSDictionary //設置控件屬性 cell.mLabel.text = rowDict["name"] as? String let imagePath = String(format: "%@.png", rowDict["image"] as! String) cell.mImage.image = UIImage(named: imagePath) cell.accessoryType = .disclosureIndicator return cell } }
示例圖:

以上就是本文的全部內容,希望對大家的學習有所幫助。