Swift - TableView

Swift - TableView 首页 / iOS入门教程 / Swift - TableView

TableView可以定义为可以在单个列中使用行排列数据的视图。它几乎用于几乎每个IOS应用程序,TableView是UITaItView类的实例,它继承UIScrollView类。无涯教程将在本教程即将举行的章节中讨论UIScrollView

class UITableView : UIScrollView

在iOS应用程序中,每当需要显示包含垂直滚动内容的单列时,无涯教程都使用tableview。tableview可以显示多个记录(分为几行),如果需要,可以垂直滚动。 tableview的每一行都显示数据源的每条记录。例如,在联系人应用程序中,无涯教程在tableview的单独行中显示每个联系人的姓名,然后单击该行即可获得与该联系人有关的详细信息。

下图显示了如何使用tableview在settings应用程序中显示数据。

无涯教程网

iOS TableView

下面的图片显示了如何在联系人应用中使用TableView。

链接:https://www.learnfk.comhttps://www.learnfk.com/ios/ios-tableview.html

来源:LearnFk无涯教程网

iOS TableView

在iOS应用程序中,tableview用于与导航控制器的关联,以按层次组织数据。在这里,无涯教程可以使用导航控制器在不同层次的层次之间进行导航。

tableview的样式由继承自UIScrollView的UITableView类管理。在tableview中,该行由UITableViewCell类的对象模拟,该类可用于显示实际内容。无涯教程可以自定义tableview单元以显示iOS应用程序中的任何内容。

添加UITableView

要将TableView添加到Storyboard,请在对象库中搜索TableView并将结果拖动到Storyboard。

iOS TableView

要使用tableView,无涯教程需要设置其委托和数据源属性。 TableView是一个数据驱动对象,即,它获取要从数据源对象显示的数据。在实际应用程序中,数据源对象包含由数据库服务器的API调用返回的数据。

可以通过在ViewController的viewDidLoad方法中使用以下代码行来设置tableview的委托和数据源属性。

tableView.delegate = self
tableView.datasource = self

委托事件

    SNMethodDescription
    1func tableView(UITableView, willDisplay: UITableViewCell, forRowAt: IndexPath)当tableview要为特定行绘制单元格时,会执行该方法。
    3func tableView(UITableView, willSelectRowAt: IndexPath) -> IndexPath?当要选择指定的行时,会执行该方法。
    4func tableView(UITableView, didSelectRowAt: IndexPath)选择tableview的指定行时,会执行该方法。
    5func tableView(UITableView, willDeselectRowAt: IndexPath) -> IndexPath?当特定的行即将被取消选择时,会执行该方法。
    6func tableView(UITableView, didDeselectRowAt: IndexPath)取消选择特定行时,会执行该方法。
    7func tableView(UITableView, viewForHeaderInSection: Int) -> UIView?此方法返回一个UI View,表示tableview的标题。
    8func tableView(UITableView, viewForFooterInSection: Int) -> UIView?此方法返回UIView,表示tableview的页脚。
    9func tableView(UITableView, willDisplayHeaderView: UIView, forSection: Int)当特定部分的headerView时,会执行该方法。
    10func tableView(UITableView, willDisplayFooterView: UIView, forSection: Int)当要显示特定节的页脚视图时,会执行该方法。
    11func tableView(UITableView, heightForRowAt: IndexPath) -> CGFloat此方法返回行的高度。
    12func tableView(UITableView, heightForHeaderInSection: Int) -> CGFloat此方法返回tableview中标题的高度。
    13func tableView(UITableView, heightForFooterInSection: Int) -> CGFloat此方法返回tableview中特定节的页脚高度。
    14func tableView(UITableView, estimatedHeightForRowAt: IndexPath) -> CGFloat此方法返回在特定位置的行的估计高度。
    15func tableView(UITableView, estimatedHeightForHeaderInSection: Int) -> CGFloat此方法返回特定位置标头的估计高度。
    16func tableView(UITableView, estimatedHeightForFooterInSection: Int) -> CGFloat此方法返回特定部分中页脚的估计高度。

    数据源方法

    要将要显示的数据保持在TableView,无涯教程需要维护实现UITableViewDataSource协议的数据源对象。 DataSource对象管理TableView数据。

      要执行上述任务,请在UITableViewDataSource协议中定义了一些函数。下表包含协议中定义的重要方法。

      SNMethodDescription
      1func tableView(UITableView, numberOfRowsInSection: Int) -> Int此方法返回要在tableView的部分中显示的行数。
      2func numberOfSections(in: UITableView) -> Int此方法返回要在tableView中显示的节数。
      3func tableView(UITableView, cellForRowAt: IndexPath) -> UITableViewCell此方法返回UITableViewCell的对象,该对象显示tableview中特定行的实际内容。此方法为tableview中的特定行插入单元格。
      4func tableView(UITableView, titleForHeaderInSection: Int) -> String?此方法返回一个字符串,该字符串表示tableview部分中的标题的标题。
      5func tableView(UITableView, titleForFooterInSection: Int) -> String?此方法返回一个表示tableview部分中的页脚标题的字符串。
      7func tableView(UITableView, canEditRowAt: IndexPath) -> Bool它要求数据源验证特定行是否可编辑。
      8func tableView(UITableView, canMoveRowAt: IndexPath) -> Bool它要求数据源验证是否可以将特定行移动到tableview中的另一个位置。
      9func tableView(UITableView, moveRowAt: IndexPath, to: IndexPath)此方法将特定行移动到tableview中的其他位置。
      10func sectionIndexTitles(for: UITableView) -> [String]?它返回包含tableview中各节标题的字符串数组。

      如果ViewController实现UITableViewDatasource协议,则需要定义两种方法,以下代码中提到了该方法。

      extension ViewController : UITableViewDataSource{
          
          func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
              return dataSourceArr.count
              
          }
          
          func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
              let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
              cell.textLabel?.text = "cell text"
                     
              return cell
          }
          
      }

      例1 - 在此示例中,无涯教程将创建一个简单的TableView,它于2019年显示前10名编程语言的列表。在此示例中,无涯教程将使用UITableView来创建接口构建器和委托和数据源方法来设置TableView中的数据,无涯教程还将使用Label对象显示TableView的标题。

      iOS TableView

      ViewController.swift

      在ViewController.swift中,无涯教程将定义委托和数据源方法来显示TableView数据。

      import UIKit
      class ViewController: UIViewController {
      
      
          @IBOutlet weak var tableView: UITableView!
          var dataSourceArr = Array<String>()
          
          override func viewDidLoad() {
              super.viewDidLoad()
             //加载视图后进行任何其他设置。
              tableView.delegate = self
              tableView.dataSource = self
              dataSourceArr = ["Python","JavaScript","Java","Swift","GoLang","C#","C++","Scala"]
          }
      }
      
      
      extension ViewController : UITableViewDelegate{
          
      }
      
      
      extension ViewController : UITableViewDataSource{
          
          func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
              return dataSourceArr.count
              
          }
          
          func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
              let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
              cell.textLabel?.text = dataSourceArr[indexPath.row]
              cell.textLabel?.textAlignment = .center
              return cell
          }
      }

      输出

      iOS TableView

      示例:处理TableView中的多个部分

      在此示例中,无涯教程将在TableView中创建多个部分,并且无涯教程将根据特定部分定义可变数量的行和行内容。无涯教程需要添加TableView并为TableView添加单元格。

      iOS TableView

      ViewController.swift

      import UIKit
      
      
      class ViewController: UIViewController {
      
      
          @IBOutlet weak var tableView: UITableView!
          
          override func viewDidLoad() {
              super.viewDidLoad()
             
              tableView.delegate = self
              tableView.dataSource = self
          }
          
      }
      
      
      extension ViewController: UITableViewDataSource{
          
          func numberOfSections(in tableView: UITableView) -> Int {
              return 3
          }
          
          func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
              if section == 0{
                  return 2
              }
              else if section == 1{
                  return 3
              }
              else if section == 2{
                  return 4
              }
              return 0
          }
          
          func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
              let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
              if(indexPath.section == 0){
                  cell.textLabel?.text = "Row in section 1"
              }
              else if(indexPath.section == 1){
                  cell.textLabel?.text = "Row in section 2"
              }
              else if(indexPath.section == 2){
                  cell.textLabel?.text = "Row in section 3"
              }
              return cell
          }
      }
      
      
      extension ViewController : UITableViewDelegate{
          
          func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
              return "Section " + (section+1).description
          }
          
      }

      示例2:自定义单元格

      无涯教程需要将TabesView添加到视图控制器并将原型单元添加到其中。在内容视图原型单元格中,无涯教程将添加一个UIView,无涯教程将添加UIImageView和UILabel对象。

      iOS TableView

      mytableviewcell.swift

      MyTableViewCell继承了UITableViewCell类,该类被分配给TableView的单元格。在此类中,无涯教程可以实例化图像视图和标签对象。

      import UIKit
      
      
      class MyTableViewCell: UITableViewCell {
      
      
          @IBOutlet weak var titleImg: UIImageView!
          
          @IBOutlet weak var titleLbl: UILabel!
          
          
          override func awakeFromNib() {
              super.awakeFromNib()
             //Initialization code
          }
      
      
          override func setSelected(_ selected: Bool, animated: Bool) {
              super.setSelected(selected, animated: animated)
      
      
             //为选定状态配置视图
          }
      
      
      }

      ViewController.swift

      import UIKit
      
      class ViewController: UIViewController {
          
          var imgArr = ["Product1","Product2","Product3","Product4","Product5","Product6","Product7","Product8"]
          var lblTextArr = ["Powerbanks","Storage Devices","LED Bulbs","Laptop Bags","Keyboards","Routers","Shoes"]
          
          @IBOutlet weak var tableView: UITableView!
          
          
          override func viewDidLoad() {
              super.viewDidLoad()
             //加载视图后进行任何其他设置。
              
              tableView.delegate = self
              tableView.dataSource = self
          }
          
      }
      
      
      
      
      extension ViewController : UITableViewDataSource{
          
          public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
              return imgArr.count - 1
          }
          
          public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
              let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyTableViewCell
              cell.titleImg?.image = UIImage(named: imgArr[indexPath.row])
              cell.titleLbl.text = lblTextArr[indexPath.row]
              return cell
          }
      }
      
      
      extension UIViewController : UITableViewDelegate{
          
          public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
              return 150
          }
          
      }
      iOS TableView

      祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

      技术教程推荐

      TensorFlow快速入门与实战 -〔彭靖田〕

      重学前端 -〔程劭非(winter)〕

      从0开发一款iOS App -〔朱德权〕

      Node.js开发实战 -〔杨浩〕

      NLP实战高手课 -〔王然〕

      WebAssembly入门课 -〔于航〕

      如何落地业务建模 -〔徐昊〕

      程序员的个人财富课 -〔王喆〕

      大厂广告产品心法 -〔郭谊〕

      好记忆不如烂笔头。留下您的足迹吧 :)