TableView可以定义为可以在单个列中使用行排列数据的视图。它几乎用于几乎每个IOS应用程序,TableView是UITaItView类的实例,它继承UIScrollView类。无涯教程将在本教程即将举行的章节中讨论UIScrollView。
class UITableView : UIScrollView
在iOS应用程序中,每当需要显示包含垂直滚动内容的单列时,无涯教程都使用tableview。tableview可以显示多个记录(分为几行),如果需要,可以垂直滚动。 tableview的每一行都显示数据源的每条记录。例如,在联系人应用程序中,无涯教程在tableview的单独行中显示每个联系人的姓名,然后单击该行即可获得与该联系人有关的详细信息。
下图显示了如何使用tableview在settings应用程序中显示数据。
下面的图片显示了如何在联系人应用中使用TableView。
链接:https://www.learnfk.comhttps://www.learnfk.com/ios/ios-tableview.html
来源:LearnFk无涯教程网
在iOS应用程序中,tableview用于与导航控制器的关联,以按层次组织数据。在这里,无涯教程可以使用导航控制器在不同层次的层次之间进行导航。
tableview的样式由继承自UIScrollView的UITableView类管理。在tableview中,该行由UITableViewCell类的对象模拟,该类可用于显示实际内容。无涯教程可以自定义tableview单元以显示iOS应用程序中的任何内容。
要将TableView添加到Storyboard,请在对象库中搜索TableView并将结果拖动到Storyboard。
要使用tableView,无涯教程需要设置其委托和数据源属性。 TableView是一个数据驱动对象,即,它获取要从数据源对象显示的数据。在实际应用程序中,数据源对象包含由数据库服务器的API调用返回的数据。
可以通过在ViewController的viewDidLoad方法中使用以下代码行来设置tableview的委托和数据源属性。
tableView.delegate = self tableView.datasource = self
SN | Method | Description |
---|---|---|
1 | func tableView(UITableView, willDisplay: UITableViewCell, forRowAt: IndexPath) | 当tableview要为特定行绘制单元格时,会执行该方法。 |
3 | func tableView(UITableView, willSelectRowAt: IndexPath) -> IndexPath? | 当要选择指定的行时,会执行该方法。 |
4 | func tableView(UITableView, didSelectRowAt: IndexPath) | 选择tableview的指定行时,会执行该方法。 |
5 | func tableView(UITableView, willDeselectRowAt: IndexPath) -> IndexPath? | 当特定的行即将被取消选择时,会执行该方法。 |
6 | func tableView(UITableView, didDeselectRowAt: IndexPath) | 取消选择特定行时,会执行该方法。 |
7 | func tableView(UITableView, viewForHeaderInSection: Int) -> UIView? | 此方法返回一个UI View,表示tableview的标题。 |
8 | func tableView(UITableView, viewForFooterInSection: Int) -> UIView? | 此方法返回UIView,表示tableview的页脚。 |
9 | func tableView(UITableView, willDisplayHeaderView: UIView, forSection: Int) | 当特定部分的headerView时,会执行该方法。 |
10 | func tableView(UITableView, willDisplayFooterView: UIView, forSection: Int) | 当要显示特定节的页脚视图时,会执行该方法。 |
11 | func tableView(UITableView, heightForRowAt: IndexPath) -> CGFloat | 此方法返回行的高度。 |
12 | func tableView(UITableView, heightForHeaderInSection: Int) -> CGFloat | 此方法返回tableview中标题的高度。 |
13 | func tableView(UITableView, heightForFooterInSection: Int) -> CGFloat | 此方法返回tableview中特定节的页脚高度。 |
14 | func tableView(UITableView, estimatedHeightForRowAt: IndexPath) -> CGFloat | 此方法返回在特定位置的行的估计高度。 |
15 | func tableView(UITableView, estimatedHeightForHeaderInSection: Int) -> CGFloat | 此方法返回特定位置标头的估计高度。 |
16 | func tableView(UITableView, estimatedHeightForFooterInSection: Int) -> CGFloat | 此方法返回特定部分中页脚的估计高度。 |
要将要显示的数据保持在TableView,无涯教程需要维护实现UITableViewDataSource协议的数据源对象。 DataSource对象管理TableView数据。
要执行上述任务,请在UITableViewDataSource协议中定义了一些函数。下表包含协议中定义的重要方法。
SN | Method | Description |
---|---|---|
1 | func tableView(UITableView, numberOfRowsInSection: Int) -> Int | 此方法返回要在tableView的部分中显示的行数。 |
2 | func numberOfSections(in: UITableView) -> Int | 此方法返回要在tableView中显示的节数。 |
3 | func tableView(UITableView, cellForRowAt: IndexPath) -> UITableViewCell | 此方法返回UITableViewCell的对象,该对象显示tableview中特定行的实际内容。此方法为tableview中的特定行插入单元格。 |
4 | func tableView(UITableView, titleForHeaderInSection: Int) -> String? | 此方法返回一个字符串,该字符串表示tableview部分中的标题的标题。 |
5 | func tableView(UITableView, titleForFooterInSection: Int) -> String? | 此方法返回一个表示tableview部分中的页脚标题的字符串。 |
7 | func tableView(UITableView, canEditRowAt: IndexPath) -> Bool | 它要求数据源验证特定行是否可编辑。 |
8 | func tableView(UITableView, canMoveRowAt: IndexPath) -> Bool | 它要求数据源验证是否可以将特定行移动到tableview中的另一个位置。 |
9 | func tableView(UITableView, moveRowAt: IndexPath, to: IndexPath) | 此方法将特定行移动到tableview中的其他位置。 |
10 | func 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的标题。
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 } }
输出
示例:处理TableView中的多个部分
在此示例中,无涯教程将在TableView中创建多个部分,并且无涯教程将根据特定部分定义可变数量的行和行内容。无涯教程需要添加TableView并为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对象。
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 } }
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)