您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 開發:表視圖(UITableView)完全詳解

編輯:iOS 開發綜合

這一篇詳細介紹表視圖的用法,從組成結構、相關類別到實際範例,一次講清楚。

概述與組成

表視圖是 iOS 開發中最重要的視圖之一,以列表形式展示資料。它由以下幾部分組成:

  • 表頭視圖:表視圖最上方的視圖
  • 表腳視圖:表視圖最下方的視圖
  • 單元格(cell):表視圖中每一行的視圖
  • 節(section):由多個單元格組成,用於分組列表,並可再細分為節頭與節腳

相關類別

UITableView 繼承自 UIScrollView,並採用兩個協議:UITableViewDelegate 與 UITableViewDataSource。此外,UITableViewCell 是單元格類別,UITableViewController 是 UITableView 的控制器,UITableViewHeaderFooterView 則用來為節頭與節腳提供視圖。

表視圖分類

  • 普通表視圖:主要用於動態表,適合單元格數量未知的情況
  • 分組表視圖:一般用於靜態表,常用來進行介面佈局

單元格的組成與樣式

單元格由圖示、主標題、副標題、擴展視圖組成,可視需要選用。內建的擴展視圖在枚舉類型中定義:

Swift 枚舉成員 Objective-C 枚舉成員 說明 none ITableViewCellAccessoryNone 沒有擴展圖示 disclosureIndicator UITableViewCellAccessoryDisclosureIndicator 擴展指示器,為箭頭 detailDisclosureButton UITableViewCellAccessoryDetailDisclosureButton 細節展示圖,為箭頭加問號 checkmark UITableViewCellAccessoryCheckmark 選中標誌,圖示為勾 detailButton UITableViewCellAccessoryDetailButton 細節詳情展示,圖示為問號

內建的單元格樣式則定義在枚舉類型 UITableViewCellStyle 中:

Swift 枚舉成員 Objective-C 枚舉成員 說明 default UITableViewCellStyleDefault 默認樣式 subtitle UITableViewCellStyleSubtitle 有圖示、主標題、副標題,副標題在主標題下方 value1 UITableViewCellStyleValue1 有主標題、副標題,主標題左對齊、副標題右對齊,可有圖示 value2 UITableViewCellStyleValue2 有主標題、副標題,兩者居中對齊,無圖示

數據源協議與委託協議

UITableViewDataSource 主要為表視圖提供資料,主要方法如下:

方法 返回類型 說明 tableView(UITableView, cellForRowAt: IndexPath) UITableViewCell 為表視圖單元格提供資料,必須實作 tableView(UITableView, numberOfRowsInSection: Int) Int 返回某個節中的行數,必須實作 tableView(UITableView, titleForHeaderInSection: Int) String 返回節頭的標題 tableView(UITableView, titleForFooterInSection: Int) String 返回節腳的標題 numberOfSections(in: UITableView) Int 返回節的個數 sectionIndexTitles(for: UITableView) [String]? 返回節索引標題

UITableViewDelegate 主要用來設定節頭、節腳的標題以及處理各種動作事件,主要方法如下:

方法 返回類型 說明 tableView(UITableView, didSelectRowAt: IndexPath) 單元格響應事件 tableView(UITableView, accessoryButtonTappedForRowWith: IndexPath) 擴展視圖響應事件

以 UIViewController 實現表視圖

步驟:

  1. 建立一個 iOS 專案
  2. 從物件庫拖入一個 TableView 到 storyboard,並讓它覆蓋整個 View
  3. 打開 Table View 的屬性檢查器,把 PrototypeCells 的值設為 1(注意不要設多個,否則會出錯),此時會產生一個 Table View Cell
  4. 打開 Table View Cell 的屬性檢查器,設定 Identifier
  5. 註冊 UITableViewDataSource 與 UITableViewDelegate 協議
  6. 撰寫程式碼實現功能

實作如下:

 // // 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 模式:

以 UITableViewController 實現表視圖

步驟:

  1. 建立一個 iOS 專案
  2. 刪除 storyboard 中 View Controller Scene 的 View Controller,再從物件庫拖入一個 Table View Controller 到設計介面
  3. 打開 Table View Controller 屬性檢查器,勾選 Is Initial View Controller,否則啟動後會黑屏
  4. 把 ViewController 類別的父類由 UIViewController 改為 UITableViewController
  5. 打開 View Controller 的屬性選擇器,在 Class 清單中選擇 ViewController
  6. UITableViewController 預設已註冊兩個協議,不需再手動註冊

實作如下:

 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 模式:

自定義單元格

步驟:

  1. 建立一個表視圖專案
  2. 把根視圖控制器改為 UITableViewController(參照上一節步驟)
  3. 從物件庫拖入控制項到單元格內部,例如 Label 與 ImageView
  4. 建立自定義單元格類別 CustomCell,並繼承 UITableViewCell
  5. 在設計介面選取 Table View Cell,打開屬性檢查器,把 Class 設為 CustomCell,並設定單元格 Identifier
  6. 為單元格內的 Label、ImageView 連接輸出接口,綁定到 CustomCell
  7. 打開 ViewController 類別,撰寫程式碼實現

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 } } 

示例圖:

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

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved