iOS開發(fā)學(xué)習(xí)TableView展現(xiàn)一個list實例
TableView 基礎(chǔ)
本文講講TableView的基本使用. 順便介紹一下delegation.
TableView用來做什么
TableView用來展示一個很長的list. 和Android中的RecyclerView不同, iOS中的TableView只能是豎直方向的list.
如何寫一個最簡單的TableView
一個最簡單的TableViewController看起來像這樣:
class ViewController: UITableViewController { var data: [String] = [] override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. // loadData() print(data) } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { data.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) cell.textLabel?.text = data[indexPath.row] return cell } }
這里data是想展示的數(shù)據(jù)類型, 可以hardcode一些數(shù)據(jù).
這么簡單是因為這個ViewController繼承了UITableViewController
, 并且cell的部分使用了storyboard.
這里需要用dequeueReusableCell
方法, 是為了cell的復(fù)用, 因為list內(nèi)容很多的時候cell view是可以循環(huán)使用的. (很像Android里的RecyclerView).
UITableViewController
的簽名是這樣:
open class UITableViewController : UIViewController, UITableViewDelegate, UITableViewDataSource {
它為我們做了以下三件事:
- 設(shè)置view為一個
UITableView
. - 設(shè)置
delegate=self
. - 設(shè)置
dataSource=self
.
這種方式的局限性在于第一點, 它的根view是一個TableView, 如果我們的需求比較復(fù)雜, 不僅僅是一個demo, 那么可能需要組合View.
拆解版TableView
我們也可以直接繼承UIViewController
類, 然后自己動手做上面的幾條設(shè)置.
Delegate & DataSource
TableView有兩個重要的方面需要關(guān)注:
- UITableViewDelegate: 管理和用戶的交互, 比如選擇, 滑動手勢等. 沒有必須要實現(xiàn)的方法.
- UITableViewDataSource: 提供和管理數(shù)據(jù), 包括了數(shù)據(jù)對應(yīng)的cell或者header. 有兩個必須要實現(xiàn)的方法(如上面的代碼例子所示).
繼承UIViewController
繼承UIViewController而不是UITableViewController
之后, 需要自己寫一個tableView并加在view里. 再分別實現(xiàn)UITableViewDelegate
和UITableViewDataSource
, 這里寫在extension里, 拆分完之后set給tableView:
tableView.delegate = self tableView.dataSource = self
整體改造后代碼如下:
class ViewController: UIViewController { var data: [String] = ["Hello", "World"] private let tableView = UITableView() override func loadView() { view = UIView() view.addSubview(tableView) tableView.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ tableView.topAnchor.constraint(equalTo: view.topAnchor), tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor), tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor), tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor), ]) } override func viewDidLoad() { super.viewDidLoad() tableView.register(MyCell.self, forCellReuseIdentifier: "MyCell") tableView.delegate = self tableView.dataSource = self } } extension ViewController: UITableViewDelegate {} extension ViewController: UITableViewDataSource { func tableView(_: UITableView, numberOfRowsInSection _: Int) -> Int { data.count } func tableView(_: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as? MyCell { cell.configure(with: data[indexPath.row]) return cell } return UITableViewCell() } }
自己的Cell class
這里Cell也改用代碼類, 寫一個這樣的類:
class MyCell: UITableViewCell { private let label = UILabel() override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) contentView.addSubview(label) label.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ label.topAnchor.constraint(equalTo: contentView.topAnchor), label.bottomAnchor.constraint(equalTo: contentView.bottomAnchor), label.leadingAnchor.constraint(equalTo: contentView.leadingAnchor), label.trailingAnchor.constraint(equalTo: contentView.trailingAnchor), ]) } @available(*, unavailable) required init?(coder _: NSCoder) { fatalError("init(coder:) has not been implemented") } func configure(with data: String) { label.text = data } }
注意tableView注冊這個Cell類型:
override func viewDidLoad() { super.viewDidLoad() tableView.register(MyCell.self, forCellReuseIdentifier: "MyCell") }
補充知識: Delegation
上面的方法初看可能會非常怪. 這里還涉及到了一個知識點是iOS中的delegate. 它存在的意義是為了拓展本身類的功能.
Apple自己的很多API就用了delegate protocol, 比如UIApplicationDelegate
, UITableViewDelegate
. 如果我們想自己定義一個:
protocol MyTypeDelegate: AnyObject { func myType(_ myType: MyType, shouldDoSomething argumentString: String) -> Bool func myType(_ myType: MyType, didAbortWithError error: Error) func myTypeDidFinish(_ myType: MyType) } class MyType { weak var delegate: MyTypeDelegate? }
定義delegation的幾個原則:
- 方法名以被代理的類型開頭.
- 方法的第一個參數(shù)是被代理的對象.
References
以上就是iOS系列學(xué)習(xí)TableView展現(xiàn)一個list實例的詳細內(nèi)容,更多關(guān)于iOS TableView展現(xiàn)list的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
iOS App開發(fā)中UIViewController類的使用教程
UIViewController是iOS中控制視圖的關(guān)鍵所在,這里我們將針對UIViewController的聲明周期與主要屬性和方法,來總結(jié)iOS App開發(fā)中UIViewController類的使用教程2016-07-07iOS11實現(xiàn)App內(nèi)自動連接Wi-Fi的方法
這篇文章主要給大家介紹了關(guān)于iOS11實現(xiàn)App內(nèi)自動連接Wi-Fi的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10Objective-C之Category實現(xiàn)分類示例詳解
這篇文章主要為大家介紹了Objective-C之Category實現(xiàn)分類示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08iOS使用pageViewController實現(xiàn)多視圖滑動切換
這篇文章主要為大家詳細介紹了iOS使用pageViewController實現(xiàn)多視圖滑動切換,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06iOS 實現(xiàn)簡單的加載等待動畫示例(思路與實現(xiàn))
本篇文章主要介紹了iOS 實現(xiàn)簡單的加載等待動畫示例(思路與實現(xiàn)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05